-- 方法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圖表知識
日進一步--昨天課間開始看書了,
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/550820.html
標籤:MySQL
下一篇:返回列表
