有关数据库表的操作

注意:以下所有命令在cmd环境下都是以“;”结束当前命令

查看某数据库下所有的数据库表:use studentcourse

                                                      show tables;

mysql> use studentcourse
Database changed
mysql> show tables;
+-------------------------+
| Tables_in_studentcourse |
+-------------------------+
| c_student               |
| course                  |
| sc                      |
| student                 |
| test_log                |
+-------------------------+
5 rows in set (0.00 sec)

创建数据库表: 

create table course(
    -> Sid int,
    -> Sname varchar(10),
    -> class varchar(20));

mysql> use student;
Database changed
mysql> create table course(
    -> Sid int,
    -> Sname varchar(10),
    -> class varchar(20));
Query OK, 0 rows affected (0.53 sec)

mysql>

查看表结构:desc 表名

mysql> desc course;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| Sid   | int(11)     | YES  |     | NULL    |       |
| Sname | varchar(10) | YES  |     | NULL    |       |
| class | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.16 sec)

添加字段:alter table 表名 add 字段名 字段类型;

mysql> alter table course add courseid int;
Query OK, 0 rows affected (0.84 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc course;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| Sid      | int(11)     | YES  |     | NULL    |       |
| Sname    | varchar(10) | YES  |     | NULL    |       |
| class    | varchar(20) | YES  |     | NULL    |       |
| courseid | int(11)     | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.10 sec)

删除字段:alter table 表名 drop 字段名

mysql> alter table course drop courseid;
Query OK, 0 rows affected (0.63 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc course;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| Sid   | int(11)     | YES  |     | NULL    |       |
| Sname | varchar(10) | YES  |     | NULL    |       |
| class | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.03 sec)

修改表的字段类型:alter table 表名 modify 字段名 字段类型;

mysql> alter table course modify class int;
Query OK, 0 rows affected (0.92 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc course;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| Sid   | int(11)     | YES  |     | NULL    |       |
| Sname | varchar(10) | YES  |     | NULL    |       |
| class | int(11)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.04 sec)

修改字段名: alter table 表名 change 旧字段名 新字段名 新字段类型;

mysql> alter table course change class Scourse varchar(10);
Query OK, 0 rows affected (0.84 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc course;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| Sid     | int(11)     | YES  |     | NULL    |       |
| Sname   | varchar(10) | YES  |     | NULL    |       |
| Scourse | varchar(10) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.04 sec)

修改表名:alter table 旧名 rename to 新名;

mysql> alter table course rename to class;
Query OK, 0 rows affected (0.22 sec)

mysql> show tables;
+-------------------+
| Tables_in_student |
+-------------------+
| class             |
+-------------------+
1 row in set (0.00 sec)

删除数据库表:drop table 数据库表名;

mysql> drop table class;
Query OK, 0 rows affected (0.19 sec)

mysql> show tables;
Empty set (0.00 sec)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值