-
常用的约束
-
主键约束/自动增长
-
外键约束
-
唯一性约束
-
非空约束
-
查看约束信息
-
添加数据
-
默认值
-
更新数据
-
删除数据
-
清除表的所有数据
/*
整数 int
浮点数 float(m,d) double(m,d) m是数字的总个数,d是小位数
字符类型 varchar(n) 设置字符的最大个数
字符类型 text 当不知道属性的最大长度,适合用text,最大65535个字符
日期类型 datatime YYYY-MM-DD HH:MM:SS
*/
1.常用的约束
# 常用的约束
/*
1.主键约束
2.外键约束
3.唯一性约束
4.非空约束
5.检查约束
*/
2.主键约束/自动增长
# 添加主键约束
alter table tb_name add primary key(column_name);
# 主键自增长
# 只能作用于整数类型
# 必须有主键/唯一性约束
# 只能作用于一列
alter table tb_name modify column_name type auto_increment;
# 解除自动增长
alter table tb_name modify column_name type;
# 解除外键约束
alter table tb_name drop primary key;
3.外键约束
# 查看外键
show create table tb_name;
# 添加外键约束
alter table tb1 add constraint 约束名 foreign key(column1_name) references tb2(column2_name);
# 删除外键
alter table tb_name drop foreign key 约束名;
4.唯一性约束
# 添加唯一性约束
alter table tb_name add constraint 约束名 unique(column_name);
# 删除唯一性约束
alter table tb_name drop key 约束名;
5.非空约束
# 添加非空约束
alter table tb_name modify column_name type not null;
# 删除非空约束
alter table tb_name modify column_name type null;
6.查看约束信息
1.show keys from tb_name;
2.show create table tb_name;
7.添加数据
# 添加部分数据
insert into tb_name(col1,col2……) values(val1,val2……);
# 添加一行数据
# 对于自动增长数据用default null 0占位
insert into tb_name values(val1,val2……);
9.默认值
# 创建时指定默认值
create table tb_name(id int primary key auto_increment,name varchar(10) default "Unknown");
# 添加行时指定默认值
alter table tb_name add column col_name type default value;
9.更新数据
# 更新数据
update tb_name set col1=val1,col2=val2 where id=1;
10.删除数据
# 删除数据
delete from tb_name where id = 1;
11.清除表的所有数据
# 清除所有数据
truncate table tb_name;
10万+

被折叠的 条评论
为什么被折叠?



