1.查看當前數據庫的所有表
show tables;
2.創建表
create table 數據表名字 (字段 類型 約束[, 字段 類型 約束]);
auto_increment 表示自動增長
多個約束,不分先後順序
設置主鍵,可以不寫not null
-- 創建表 students
create table students(
id int unsigned primary key auto_increment not null,
name varchar(10) not null,
age tinyint unsigned default 0,
gender enum('男','女','中性','保密') default '保密',
class_id int unsigned not null
);
3.查看創建的表
show create table students;
4.查看錶結構—使用頻率相當高
desc students;
5.修改表結構—alter操作
- 添加字段 alter table 表名 add 列名 類型/約束;
alter table students add birthday datetime default "2011-11-11 11:11:11";
- 修改字段:不改變字段名,僅改變類型
alter table students modify birthday date default "2011-11-11";
- 修改字段:改變字段名(alter table 表名 change 原列名 新列名 類型及約束;)
alter table students change birthday birth date default "2011-11-11";
- 刪除字段
alter table students drop birth;
- 刪除表xx
drop table xx;