增
語法:
// 默認新增的列在表的最後一列
ALTER TABLE table_name
ADD column_name datatype
// 在指定列名後添加新列
ALTER TABLE table_name
ADD column_name datatype after 列名
例子:
// 默認新增的列在表的最後一列
alter table user
add height tinyint unsigned not null default 0
// 在指定列名後添加新列
alter table user
add height tinyint unsigned not null default 0 after weight
刪
語法:
// 刪庫
DROP DATABASE database_name
// 刪表
DROP TABLE table_name
// 刪除表內的數據,但並不刪除表本身(相當於刪除表再重建)
TRUNCATE TABLE table_name
// 刪除表中的索引
ALTER TABLE table_name DROP INDEX index_name
// delete 語句用於刪除表中的數據
delete from 表名稱 where 刪除條件;
刪除 id 爲 3 的行: delete from students where id=3
// 刪列
ALTER TABLE table_name
DROP COLUMN 列名
例子:
alter table user
drop column height
delete,drop,truncate 都有刪除表的作用,區別在於:
1、delete 和 truncate 僅僅刪除表數據,drop 連表數據和表結構一起刪除,打個比方,delete 是單殺,truncate 是團滅,drop 是把電腦摔了。
2、delete 是 DML 語句,操作完以後如果沒有不想提交事務還可以回滾,truncate 和 drop 是 DDL 語句,操作完馬上生效,不能回滾,打個比方,delete 是發微信說分手,後悔還可以撤回,truncate 和 drop 是直接扇耳光說滾,不能反悔。
3、執行的速度上,drop>truncate>delete,打個比方,drop 是神舟火箭,truncate 是和諧號動車,delete 是自行車。
改
例子:
// change 可以直接修改列名
alter table user
change height shengao smallint
// modify 不能修改列名
alter table user
modify shengao height smallint(失敗)
查
desc 表名