欢迎投稿

今日深度:

mysql索引--普通索引,唯一索引,主键索引,参照完整性约束,数据完整性约束,--其中:工号cha

mysql索引--普通索引,唯一索引,主键索引,参照完整性约束,数据完整性约束,--其中:工号cha


-- 方法1:create index
-- 对employee表的员工部门号列创建普通索引depart_ind
-- create index depart_ind on employees(员工部门号);

-- 对employee表的姓名和地址列创建复合索引ad_ind;
-- create index ad_ind on employees(姓名,地址);

-- 对departments表的部门名称列创建唯一索引un_ind;
-- create unique index un_ind on departments(部门名称);

-- 方法2:alter table
-- 对employee表的出生日期列创建唯一索引date_ind ,姓名和性别列添加复合索引na_ind(用alter table命令完成)
-- alter table employees add unique data_ind(出生日期),add index na_ind(姓名,性别);

-- 练习:对departments表的部门编号列创建唯一索引key_ind;(用alter table)
-- alter table departments add unique key_ind(部门编号);

-- 方法3:创建表的同时创建索引
-- 创建表cpk(产品编号,产品名称,单价,库存量),并对产品编号创建主键,
-- 在库存量和单价列上创建复合索引cpk_fh
-- create table if not exists cpk(
-- 产品编号 varchar(5) primary key,产品名称 varchar(20),单价 float(4),库存量 int(4),
-- index cpk_fh(单价,库存量));

-- create table if not exists cpk2(
-- 产品编号 varchar(5),产品名称 varchar(20),单价 float(4),库存量 int(4),
-- index cpk_fh(单价,库存量),primary key(产品编号));

-- 参照完整性约束
-- create table if not exists jj(
-- employeeID char(6),je float(4),
-- foreign key(employeeID) references employees(员工编号)
-- on delete cascade on update cascade) engine=InnoDB;


-- insert into jj values("020018",78.01),("102208",123),("102201",456);
-- select * from jj;

-- delete from employees where 员工编号="102208";


-- 数据完整性
-- 创建雇员表emp,只考虑工号和性别两列,性别只能包含男或女
create table if not exists emp (工号 char(5),性别 char(2),check(性别="男" or 性别="女"));

insert into emp values("001","男"),("002","女");

-- 练习:创建表emp_3,有工号,工资和扣款3列,要求工资必须大于扣款。
-- 其中:工号 char(6) 工资 float(4),扣款 float(4)
create table if not exists emp3 (工号 char(5),工资 float(4),扣款 float(4)
,check(工资>扣款));

insert into emp3 values("001",1000,500),("002",2000,1500);

 

 日行一善--今天给我的老同学讲解excel图表知识

日进一步--昨天课间开始看书了。

www.htsjk.Com true http://www.htsjk.com/Mysql/46129.html NewsArticle mysql索引--普通索引,唯一索引,主键索引,参照完整性约束,数据完整性约束,--其中:工号cha -- 方法1:create index -- 对employee表的员工部门号列创建普通索引depart_ind -- create index depa...
评论暂时关闭