语法:
CREATE TABLE table_name (
field1 datatype,
field2 datatype,
field3 datatype
) character set 字符集 collate 校验规则 engine 存储引擎;
例子:
mysql> create table users(
-> id int comment '编号',
-> name varchar(20) comment '用户名',
-> password char(32) comment '用户密码',
-> birthday date comment '生日'
-> );
语法:
desc 表名;
语法:
ALTER TABLE tablename ADD (column datatype [DEFAULT expr][,column
datatype]...);
ALTER TABLE tablename MODIfy (column datatype [DEFAULT expr][,column
datatype]...);
ALTER TABLE tablename DROP (column);
案例:
insert into users values(1, '小郑', '4321', '2000-09-30'), (2, '小张', '1234', '1999-11-2');
alter table users add assets varchar(30) comment '图片路径' after birthday;
mysql> desc users;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| password | char(32) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| assets | varchar(30) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
插入新字段后,对原来表中的数据没有影响
mysql> select * from users;
+------+--------+----------+------------+--------+
| id | name | password | birthday | assets |
+------+--------+----------+------------+--------+
| 1 | 小郑 | 4321 | 2000-09-30 | NULL |
| 2 | 小张 | 1234 | 1999-11-02 | NULL |
+------+--------+----------+------------+--------+
2 rows in set (0.00 sec)
alter table users modify name varchar(30);
mysql> desc users;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
| password | char(32) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| assets | varchar(30) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
alter table users drop password;
mysql> desc users;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(30) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| assets | varchar(30) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
alter table users rename to emname;
alter table users change name xiaoming varchar(60);
mysql> desc users;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| xiaoming | varchar(60) | YES | | NULL | |
| birthday | date | YES | | NULL | |
| assets | varchar(30) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
语法格式:
DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ...
drop table users;