Sql基础
DDL (data defination language)
创建表
CREATE TABLE emp(
ename varchar(10),
hiredate date,
sal decimal(10,2),
deptno int(2)
);
mysql> show create table emp;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| emp | CREATE TABLE `emp` (
`ename` varchar(10) COLLATE utf8_unicode_ci DEFAULT NULL,
`hiredate` date DEFAULT NULL,
`sal` decimal(10,2) DEFAULT NULL,
`deptno` int(2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)
修改表
- 修改表类型
ALTER TABLE emp modify column ename varchar(20);
ALTER TABLE emp modify ename varchar(25);
- 增加表字段
ALTER TABLE emp add column age int (3);
- 删除表字段
ALTER TABLE emp drop column age;
- 字段改名
ALTER TABLE emp change age age1 int(5);
- 修改顺序
mysql> ALTER TABLE emp add column age int (4) AFTER ename;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc emp;
+----------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| ename | varchar(25) | YES | | NULL | |
| age | int(4) | YES | | NULL | |
| hiredate | date | YES | | NULL | |
| sal | decimal(10,2) | YES | | NULL | |
| deptno | int(2) | YES | | NULL | |
+----------+---------------+------+-----+---------+-------+