1、創建表
DROP TABLE t_student; create table t_student ( id int PRIMARY KEY NOT NULL, -- 主鍵必須唯一,不能為空 stu_no INT UNIQUE ,-- 唯一約束,可以為空,除非設置為not NULL,並且可以有多個null stu_name varchar(20) NOT NULL,-- 非空約束 -- stu_addr varchar(100) , -- stu_phone varchar(11), stu_sex SMALLINT DEFAULT 1 CHECK(stu_sex in(0,1)),-- 檢查約束,check約束 stu_age tinyint(4) NOT NULL DEFAULT 30 CHECK (stu_age BETWEEN 20 AND 60) -- 默認值約束,check約束對數據驗證沒有任何作用 )
2、插入數據
INSERT INTO t_student VALUES (2,2315,'張e三',0,25); INSERT INTO t_student VALUES (1,2314,'張三',0,26);
3、添加列
-- 添加列 ALTER TABLE t_student ADD stu_addr varchar(100);
4、刪除列
-- 刪除列 ALTER TABLE t_student DROP COLUMN stu_name;
5、修改列-修改的列必須為空,沒有數據
-- 修改列,修改的列必須為空 ALTER TABLE t_student MODIFY stu_addr tinyint(4);
6、重命名表名
-- 重命名表名 ALTER TABLE t_student RENAME TO t_stu;
7、刪除表
-- 刪除表 DROP TABLE t_stu;
8、刪除數據
-- 刪除數據 DELETE FROM t_stu WHERE stu_no = '2314'; ROLLBACK; SELECT * FROM t_stu; -- TRUNCATE不支持where條件,以及不能回滾,不過速度很快 TRUNCATE TABLE t_stu;