1.DDL
對數(shù)據(jù)庫內(nèi)部的對象進(jìn)行創(chuàng)建、刪除份蝴、修改等操作的語言,DDL語句更多的是由數(shù)據(jù)庫管理員(DBA)使用,開發(fā)人員一般很少使用
show databases;
1)創(chuàng)建數(shù)據(jù)庫
create database 數(shù)據(jù)庫名园匹;
2)刪除數(shù)據(jù)庫
drop database 數(shù)據(jù)庫名天梧;
3)創(chuàng)建表(在哪個(gè)數(shù)據(jù)庫創(chuàng)建需要先使用UER 數(shù)據(jù)庫名來選擇數(shù)據(jù)庫內(nèi)部創(chuàng)建表)
3-1)
create table 表名(
字段1名 字段1類型 列的約束條件
字段2名 字段2類型 列的約束條件
)
3-2) 查看表的定義
desc 表名;
查看表
show tables;
3-3)查看創(chuàng)建表
show create table 表名 \G
- 刪除表
drop table 表名;
5)修改表
5-1)修改表的字段類型
alter table 表名 modify [colum] 字段定義 [first|after 字段名]仲锄;
alter table class modify id tinyint;
5-2)增加表字段
alter table 表名 add [column] 字段定義 [first|after 字段名]劲妙;
alert table class add id2 int;
5-3)刪除表字段
alert table 表名 drop [column] 字段名;
alter table class drop id2;
5-4)字段改名
alter table class change id id1 int;
5-5) 修改字段排列排序
前面的介紹字段的增加和修改(add/change/modify)中儒喊,都有一個(gè)可選項(xiàng)first|after 字段名镣奋,這個(gè)選擇可以用來修改字段在表中的位置新增的字段默認(rèn)是加載在表中最后位置,而change/modify 默認(rèn)都不會改變字段的位置
alter table t1 modify id2 tinyint first;
alter table t1 modify id2 tinyint after id1;
*** 注意:change/first|after 字段名 這些關(guān)鍵字都是屬于MySQL在標(biāo)準(zhǔn)SQL上的擴(kuò)展怀愧,在其他的數(shù)據(jù)庫上不一定適用
6)更改表名
alter table 表名 rename [to] 新的表名;
alter table class rename class1; //講表class改名成class1