刚刚开始接触学习JDBC,首先涉及到的便是数据库,找到一个帖子,从MYSQL入门安装到基础语法,图多详尽,超级推荐!
MySQL数据库应用总结(https://zhuanlan.zhihu.com/seali521)
跟着这个帖子学完,基本的数据库语法和操作应该都没问题了!
总结一下下常用的命令:
1.数据库和数据库表
!!!一定要加分号啊,不然命令行不执行,无响应
1.1数据库
功能 | 代码 |
---|---|
查看所有数据库 | show databases; |
创建数据库 | create database 数据库名; |
显示创建的数据库 | show create database 数据库名\G; |
使用数据库 | use 数据库名; |
删除数据库 | drop database 数据库名; |
1.2 数据库表
功能 | 代码 |
---|---|
显示所有的表 | show tables |
建表 | create table name(field1 type constraint default ,field2 ……,field3 …… ); |
单字段主键约束 | create table name(field1 type constraint default primary key ,field2 ……,field3 …… ); |
多字段联合约束 | create table name(field1 type constraint default ,field2 ……,field3 …… ,primary key(field 1,field2)); |
外键约束 | create table name(field1 type constraint default primary key ,field2 ……,constraint foreign_filedname foreign key(fieldname) references primarytablename(primary filed)); |
非空约束 | create table name(field1 type constraint default primary key ,field2 …… not null,field3 …… ); |
直接定义 | create table name(field1 type constraint default primary key ,field2 …… not null,field3 …… unique ); |
靠后定义 | create table name(field1 type constraint default primary key ,field2 …… not null,field3 …… ,constraint sth unique(field 1,field2,…) ); |
默认值约束 | create table name(field1 type constraint default primary key ,field2 ……default,field3 …… ); |
表属性自动增加 | create table name(field1 type constraint default primary key auto_increment,field2 ……,field3 …… ); |
查看表的基本结构 | describe table_name; desc table_name; |
查看表的详细结构 | show create table table_name\G; |
修改表名 | alter table old_tablename rename new_tablename; |
修改字段的数据类型 | alter table 表名 modify 字段名 数据类型; |
修改字段名 | alter table表名 change 旧字段名 新字段名 新数据类型; |
添加无完整性约束字段 | alter table 表名add 新字段 数据类型; |
添加有完整性约束字段 | alter table 表名add 新字段 数据类型 约束条件; |
添加字段在第一列 | alter table 表名 add 字段名 数据类型 first; |
添加字段到指定列 | alter table 表名 add 字段名 数据类型 after 已存在字段名; |
删除字段 | alter table表名 drop字段名; |
修改字段为表第一个字段 | alter table 表名modify 字段名 数据类型 first; |
修改字段到指定字段后 | alter table 表名modify 字段1 数据类型 after字段2; |
更改表的存储引擎 | alter table 表名 engine=更改后的存储引擎名; |
删除表的违建约束 | alter table 表名 drop foreign key 外键约束名; |
删除数据库表 | drop table if exists 表名1,表名2,…表名n; |
修改数据表中的值 | UPDATE table_name SET field1=new-value1, field2=new-value2 [WHERE Clause] |
2.数据的增删改查
功能 | 代码 |
---|---|
为表所有字段插入数据 | insert into 表名(字段名1,字段名2,…) values (值1,值2,…); |
为表指定字段插入数据 | insert into 表名(指定字段名1,指定字段名2,…) values (值1,值2,…); |
为表同时插入多条数据 | insert into 表名(字段名1,字段名2,…) values (值1,值2,…),(值1,值2,…),…; |
将查询结果插入数据表中 | insert into 表名1 (表1字段名1,表1字段名2,…) select (表2字段名1,表2字段名2,…) from 表名2 where 查询条件; |
更新表数据 | update 表名 set 字段名1=值1, 字段名2=值2, …字段名n=值n where(更新条件); |
删除表数据 | delete from 表名 where 删除条件; |
删除表 | drop table 表名; |
删除数据库表 | drop table if exists 表名1,表名2,…表名n; |