1. 表的创建
// mysql语义优先
// 列 类型
// 表名也可以不带''
create table [if not exists] 'tablename'(
columnname1 datatype,
columnname2 datatype
) [character set 字符集 collate 校验规则 engine 存储引擎];
一般使用charset=utf8 engine=INnoDB
最后的三项内容不写的话则使用设定的默认值
2. 表的查看
2.1 查看库中所有表
show tables;
mysql> show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| ok |
| tchar |
| tcomment |
| tdefault |
| test |
| test2 |
+------------------+
2.2 查看表的结构
desc tablename;
mysql> desc ok;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| a | char(10) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
2.3 查看表的创建语句
show create table tablename;
// \G去掉特殊提示
mysql> show create table ok\G
*************************** 1. row ***************************
Table: ok
Create Table: CREATE TABLE `ok` (
`a` char(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
3. 表的修改
// 增加列
// after:可以指定插在某一列后,但是不支持before
ALTER TABLE tablename ADD (column datatype [DEFAULT expr][,column datatype]...)[after columnname];
// 修改列信息
ALTER TABLE tablename MODIfy (column datatype [DEFAULT expr][,column datatype]...);
// 删除列
alter table tablename drop (column);
// 修改表名
alter table oldname rename [to] newname;
// 修改列名
// 新字段需要完整定义字段名和数据类型等信息
alter table tablename change oldname newname datatype;
4. 表的删除
删除表的全部数据内容
drop table [if exists] tablename[,tablename2];