查詢表
1.查看數(shù)據(jù)庫(kù)中的表:show tables;
mysql>show tables;
1046 (3D000): No database selected--沒有選定數(shù)據(jù)庫(kù),要先使用use選定數(shù)據(jù)庫(kù)
mysql>use mydb;
Database changed
2.查看創(chuàng)建表的語(yǔ)法:show? create table 表名;
mysql>show create table mydb\G???????? --按字段顯示
3.查詢表結(jié)構(gòu):desc 表名;
4.創(chuàng)建表
簡(jiǎn)單表創(chuàng)建:create table `表名` (字段名 字段類型, 字段名 字段類型, ....)
復(fù)雜表創(chuàng)建:create table `表名` (字段名? 字段類型 列屬性, 字段名 字段類型 列屬性, ...)
列屬性:
not null? ? 非空
null? ? 空
auto_increment? ? 自動(dòng)增長(zhǎng)
primary key? ? 主鍵(非空,唯一)
comment? ? 備注,說(shuō)明
default `默認(rèn)值`
刪除表:drop table [if exits] `表名`, `表名`, `表名` ...;
修改表:alter table 表名 修改關(guān)鍵字
1.添加表字段: alter table 表名 add [column] 字段名 類型名 [位置]
2.刪除字段:alter table 表名 drop[column] 字段名
3.既修改字段名,同時(shí)又修改字段類型 ?!?change
語(yǔ)法:alter? table? 表名 change [column] 舊字段名 新字段名 新數(shù)據(jù)類型
4.只修改字段類型。—— modify
語(yǔ)法:alter? table? 表名 modify [column] 字段名 新數(shù)據(jù)類型
5.修改表名:
rename table? 表名 to 新表名
alter table 表名 rename to 新表名。
6.修改引擎:
語(yǔ)法: alter table 表名 engine=新引擎名
復(fù)制表
1.復(fù)制表數(shù)據(jù),不復(fù)制表屬性
create table 新表名 select * from 舊表名;
2.復(fù)制表屬性,不復(fù)制表數(shù)據(jù)
create table 新表名 like 舊表名;
3.copy表: