数据库 基础篇4(mysql语法---表)

5 表管理

选择数据库

 

  5.1 查看所有表

mysql> show tables;

+-----------------+

| Tables_in_day15 |

+-----------------+

| student         |

+-----------------+

1 row in set (0.00 sec)

 

  5.2 创建表

mysql> create table student(

    -> sid int,

    -> sname varchar(20),

    -> sage int

    -> );

Query OK, 0 rows affected (0.01 sec)

 

  5.3 查看表结构

 

mysql> desc student;

+-------+-------------+------+-----+---------+-------+

| Field | Type        | Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| sid   | int(11)     | YES  |     | NULL    |       |

| sname | varchar(20) | YES  |     | NULL    |       |

| sage  | int(11)     | YES  |     | NULL    |       |

+-------+-------------+------+-----+---------+-------+

3 rows in set (0.01 sec)

 

5.4 删除表

 

mysql> drop table student;

Query OK, 0 rows affected (0.01 sec)

 

5.5 修改表

1)添加字段

 

mysql> alter table student add column sgender varchar(2);

Query OK, 0 rows affected (0.03 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

   2)删除字段

 

mysql> alter table student drop column sgender;

Query OK, 0 rows affected (0.03 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

3)修改字段类型

 

mysql> alter table student modify column remark varchar(100);

Query OK, 0 rows affected (0.07 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

4)修改字段名称

 

mysql> alter table student change column sgender gender varchar(2);

Query OK, 0 rows affected (0.03 sec)

Records: 0  Duplicates: 0  Warnings: 0

 

5)修改表名称

 

mysql> alter table student rename to teacher;

Query OK, 0 rows affected (0.01 sec)

posted @ 2016-10-12 08:19  lifehrx  阅读(280)  评论(0编辑  收藏  举报