mysql学习
1、where和having的区别是,不满足where条件的不会进入分组阶段,不满足having条件的分组结果不会返回,一个是分组前的条件,一个是分组后的条件,且where后不能跟聚合函数
2、删除唯一约束
mysql> alter table t drop index phone;
3、添加唯一约束
mysql> alter table t modify phone varchar(20) unique;
4、创建非空约束
mysql> create table employ(id int,name varchar(20) not null);
主键约束:非空且唯一
mysql> create table t1(id int primary key,name varchar(20));--创建主键
mysql> alter table t1 drop primary key;--删除主键
mysql> alter table t1 modify id int primary key;--添加主键
mysql> create table t2(id int primary key auto_increment,name varchar(20));--主键自增
外键约束
mysql> create table emp(id int primary key auto_crement,name varchar(20),age int,depid int);
--创建员工表
mysql> create table department(id int primary key auto_increment,name varchar(20),addr varchar(20));--创建部门表
mysql> insert into department values(null,'研发部','广州'),(null,'销售部','深圳');--添加部门数据
mysql> insert into emp values(null,'老大',22,1),(null,'',23,2),(null,'老二',24,1),(null,'老三',37,2),(null,'老四',58,1),(null,'老五',39,2);--添加员工数据
mysql> alter table emp add constraint deptid foreign key (depid) reference department(id);--添加外键约束
mysql> alter table emp drop foreign key deptid;--删除外键约束
mysql> alter table emp add constraint deptid foreign key (depid) references department(id) on update cascade;--设置外键级联更新,更新外键后,所有关联的表中字段会对应更新
mysql> alter table emp add constraint deptid foreign key (depid) references department(id) on update cascade on delete cascade;--设置级联更新和级联删除