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


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

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 key,name char)

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

一對一

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

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

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

 

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

多對多

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

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

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

關鍵是關系表建立

 

 

 

 

 

 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM