MySQL-SQL基礎

mysql> use test; Database changed mysql> create table emp(ename varchar(10),hirdate date,sal decimal(10,2),deptno int(2)); Query OK, 0 rows affected (0.01 sec) mysql> show tables; +----------------+
| Tables_in_test |
+----------------+
| emp            |
+----------------+
1 row in set (0.00 sec) mysql> desc emp; +---------+---------------+------+-----+---------+-------+
| Field   | Type          | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ename   | varchar(10)   | YES  |     | NULL    |       |
| hirdate | date          | YES  |     | NULL    |       |
| sal     | decimal(10,2) | YES  |     | NULL    |       |
| deptno  | int(2)        | YES  |     | NULL    |       |
+---------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec) mysql> show create table emp\G *************************** 1. row ***************************
       Table: emp Create Table: CREATE TABLE `emp` ( `ename` varchar(10) DEFAULT NULL, `hirdate` date DEFAULT NULL, `sal` decimal(10,2) DEFAULT NULL, `deptno` int(2) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec) mysql> alter table emp modify ename varchar(20); Query OK, 0 rows affected (0.01 sec) Records: 0  Duplicates: 0  Warnings: 0 mysql> desc emp; +---------+---------------+------+-----+---------+-------+
| Field   | Type          | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ename   | varchar(20)   | YES  |     | NULL    |       |
| hirdate | date          | YES  |     | NULL    |       |
| sal     | decimal(10,2) | YES  |     | NULL    |       |
| deptno  | int(2)        | YES  |     | NULL    |       |
+---------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec) mysql> alter table emp add column age int(3); Query OK, 0 rows affected (0.02 sec) Records: 0  Duplicates: 0  Warnings: 0 mysql> desc emp; +---------+---------------+------+-----+---------+-------+
| Field   | Type          | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ename   | varchar(20)   | YES  |     | NULL    |       |
| hirdate | date          | YES  |     | NULL    |       |
| sal     | decimal(10,2) | YES  |     | NULL    |       |
| deptno  | int(2)        | YES  |     | NULL    |       |
| age     | int(3)        | YES  |     | NULL    |       |
+---------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec) mysql> alter table emp drop column age; Query OK, 0 rows affected (0.02 sec) Records: 0  Duplicates: 0  Warnings: 0 mysql> desc emp; +---------+---------------+------+-----+---------+-------+
| Field   | Type          | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ename   | varchar(20)   | YES  |     | NULL    |       |
| hirdate | date          | YES  |     | NULL    |       |
| sal     | decimal(10,2) | YES  |     | NULL    |       |
| deptno  | int(2)        | YES  |     | NULL    |       |
+---------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)  mysql> alter table emp add column age int(3); Query OK, 0 rows affected (0.01 sec) Records: 0  Duplicates: 0  Warnings: 0 mysql> alter table emp change column age age1 int(4); Query OK, 0 rows affected (0.01 sec) Records: 0  Duplicates: 0  Warnings: 0 mysql> desc emp; +---------+---------------+------+-----+---------+-------+
| Field   | Type          | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ename   | varchar(20)   | YES  |     | NULL    |       |
| hirdate | date          | YES  |     | NULL    |       |
| sal     | decimal(10,2) | YES  |     | NULL    |       |
| deptno  | int(2)        | YES  |     | NULL    |       |
| age1    | int(4)        | YES  |     | NULL    |       |
+---------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)  mysql> alter table emp add birth date after ename; Query OK, 0 rows affected (0.02 sec) Records: 0  Duplicates: 0  Warnings: 0 mysql> mysql> desc emp; +---------+---------------+------+-----+---------+-------+
| Field   | Type          | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| ename   | varchar(20)   | YES  |     | NULL    |       |
| birth   | date          | YES  |     | NULL    |       |
| hirdate | date          | YES  |     | NULL    |       |
| sal     | decimal(10,2) | YES  |     | NULL    |       |
| deptno  | int(2)        | YES  |     | NULL    |       |
| age1    | int(4)        | YES  |     | NULL    |       |
+---------+---------------+------+-----+---------+-------+
6 rows in set (0.00 sec) mysql> alter table emp rename emp1; Query OK, 0 rows affected (0.01 sec)  mysql> desc emp1; +---------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+---------------+------+-----+---------+-------+ | ename | varchar(20) | YES | | NULL | | | birth | date | YES | | NULL | | | hirdate | date | YES | | NULL | | | sal | decimal(10,2) | YES | | NULL | | | deptno | int(2) | YES | | NULL | | | age1 | int(4) | YES | | NULL | | +---------+---------------+------+-----+---------+-------+ 6 rows in set (0.00 sec) mysql> 
相關文章
相關標籤/搜索