mysql with操作 mysql中的表操作

2021-10-25 14:51:30 字數 1581 閱讀 1951

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

建立資料庫

create database 資料庫名

切換資料庫

use 資料庫名

建表:create table 表名 (

欄位名1,型別,約束

欄位名2,型別,約束

約束:1.主鍵約束

1)直接在建表時字段型別後加 primary key

2)在表最後加 constraint 約束名 primary key(欄位名)

3)表外修改 alter table 表名 add constraint 約束名 primary key(欄位名)

2.檢查約束

1)直接在建表型別後加 check(約束條件)

2)在表最後加 constraint 約束名 check(約束條件)

3)表外修改 alter table 表名 add constraint 約束名 check(約束條件)

注:mysql不支援檢查約束,但是寫上檢查約束不會報錯

3.非空約束

1)直接在建立表的型別後加 not null

2) 在表最後加入 constraint 約束名 check(欄位名 is not null)

3)在表外修改 alter table 表名 modify 欄位名 字段型別 not null

4.唯一約束

1)直接在建立表的型別後加 unique

2) 在表的最後加入 constraint 約束名 unqiue(欄位名)

3) 在表外修改 alter table 表名 add constraint 約束名 unique(欄位名)

5.外來鍵約束

1)直接在建立表的型別後加 references 父表名(父表主鍵名)

2)在表的最後加入 constraint 約束名 foreign key(欄位名) references 父表名(父表主鍵名)

3)在表外修改 alter table 表名 add constraint 約束名 foreign key(欄位名) references 父表名(父表主鍵名)on delete set null on updata cascade

6.預設約束

1)直接在建立表的型別後加 default 預設值

2)在表外修改 alter table 表名 add constraint 約束名

刪除約束

alter table 表名 drop constraint 約束名

表的修改

1)新增字段

alter table 表名 add 欄位名 字段型別 注:在表中已經有值時,不能加非空約束

2)刪除字段

alter table 表名 drop 欄位名

3)修改字段型別

alter table 表名 modify 欄位名 新字段型別

4)修改欄位名

alter table 表名 change 欄位名 新欄位名 字段型別

5)修改表名

alter table 表名 rename as 新錶名

6)刪除表

drop table 表名

檢視當前資料庫中所有表

show tables

mysql with 遞迴 mysql遞迴

sql server可以用with as 語法,mysql沒有這個功能,只能用別的方式了,目前的mysql版本中並不支援直接的遞迴查詢,但是通過遞迴到迭代轉化的思路,還是可以在一句sql內實現樹的遞迴查詢的。這個得益於mysql允許在sql語句內使用 變數。以下是示例 建立 create table...

mysq集合差操作 Python 基礎(集合)

st set hello world 建立乙個唯一字元的集合 st type st set st set 關注 點讚 點讚 點讚 在看 st 集合物件的一些方法 方法說明set.add elmnt 增加乙個新元素到集合裡,elmnt要新增的元素。set.updata set 把set中元素更新到原集...

php中的mysql php中操作mysql

認識 1.error reporting e all e deprecated 排除所有非致命的錯誤 2.mysql connect 開啟了乙個對mysql伺服器的連線。mysql connect localhost root or die mysql error or die mysql erro...