1、create table -- 新建表
create table 表名(
欄位名1 型別[(寬度) 約束條件],
欄位名2 型別[(寬度) 約束條件],
欄位名3 型別[(寬度) 約束條件]
);
# 在同一張表中,欄位名是不能相同的
# 寬度和約束條件是可以選擇的
# 欄位名和型別是必須要有的
2、drop table -- 洗掉表
drop table 表名;
3、desc 表名 -- 查看表結構
# 查看表結構有兩種方式:
describe 表名;這種方法和desc 表名;效果相同;可以查看當前的表結構
雖然desc命令可以查看表的定義,但是其輸出的資訊還不夠全面,為了得到更全面的表定義資訊,有時候就需要查看創建表的SQL陳述句,使用 show create table 語法,除了可以看到表定義之外,還可以看到engine(存盤引擎)和charset(字符集)等資訊,(\G選項的含義是是的記錄能夠豎向排列,以便更好的顯示內容較長的記錄,)
4、alter table -- 修改表
(1)、 alter table 表名 rename 新表名; --- 修改表名
alter table test rename staff;
(2)、alter table 表名 charset 編碼; --- 修改表的編碼方式
alter table test charset utf8;
(3)、alter table 表名 auto_increment 自增的位置; --- 修改表的自增值
alter table test auto_increment = 10;
(4)、alter table 表名 add 欄位名 型別(長度) 約束; --- 增加欄位
alter table test add sex enum('male','female');
(5)、 alter table 表名 drop 欄位名; --- 洗掉欄位
alter table test drop sex;
(6)、 alter table 表名 change 欄位名 新名字 型別(長度) 約束; --- 修改欄位名
alter table test change name sname varchar(20);
(7)、 alter table 表名 modify 欄位名 新型別(新長度) 約束; --- 修改欄位型別
alter table test modify id int(4);
(8)、 alter table 表名 change 舊欄位名 新欄位名 型別(長度) 約束 frist; -- 修改欄位的排列順序為第一個
alter table test change sex sex enum('male','female') first;
(9)、 alter table 表名 change 舊欄位名 新欄位名 型別(長度) 約束 after 欄位; --- 修改欄位的排列順序為在欄位后面
alter table test change sex sex enum('male','female') after sname;
(10)、alter table 表名 add 欄位名 型別(長度) 約束 frist; --- 添加一個欄位位置在第一個
alter table test add age int first;
(11)、 alter table 表名 add 欄位名 型別(長度) 約束 after 欄位; --- 添加一個欄位在name欄位后
alter table test add hobby char(22) after cname;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/193918.html
標籤:MySQL
上一篇:C語言基礎(1)
