显示所有的表 |
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] |
网友评论