1. 程式人生 > >表,字段處理詳細, 創建用戶 ,用戶管理 表關系

表,字段處理詳細, 創建用戶 ,用戶管理 表關系

字段類型 數據類型 ces alt oca hang truncate lte cas

---恢復內容開始---

1.修改表名

alter table t1 rename t2;

2.修改表裏字段類型

alter table t1 modify name char(5);

3.修改表裏字段名

alter table t1 change name age int;

4.復制表

create table t2 like t1; 復制表的結構約束 無數據

create table t2 select *from t1; 復制表的數據與結構 無約束

create table t2 select*from t1 where 1=2(假命題) 復制表的結構

5.請空表

truncate 他

6.添加

alter table t1 add name char(5) not null first

alter table t1 add name char(5) not null after age

7.刪除字段名

alter table t1 drop name;

用戶管理

創建用戶

create user zero@localhost identified by‘zero‘;

創建權限

grant all on db1.* to zero@localhost with grant option;

一起創建

grant all on db1.* to zero@localhost identified by‘zero‘

撤銷權限

revoke delete on db1.* from zero@localhost;

修改密碼

set password for zero@localhost = password(‘123‘);

刪除用戶

drop user zero@localhost

表關系

on update cascade # 設置級聯

on delete cascade不設置級聯只能先刪除依附表一對多的多的內容,多對多都不能刪除要先刪關系表

多對一

create table school (id int primary,name char)

crsete table student(id int primary, name char, nid int,foreign key (nid) references school(id));

一對一

create table husband(id int primary, name char);

createtable wife (id int primary, nid int unique,name char,foreign key(nid) references wife(id));

一對一註意外鍵對應字段設置唯一鍵

外鍵不為主鍵,必須為對方主鍵,數據類型要一致

多對多

create table student(id int primary,name char );

create table lesson(id int primary,name char(5));

create table xuanke(id int primary, nid1 int,nid2 int,foreign key(nid1) references student(id),foreign key(nid2) references lesson(id))

關鍵是關系表建立

---恢復內容結束---

表,字段處理詳細, 創建用戶 ,用戶管理 表關系