创建表
语法:CRATE TABLE [IF NOT EXISTS] 表名(
列名 列类型
列名 列类型
··· ···
列名 列类型
);
输入语句:
mysql> CREATE TABLE tb_stu(
-> number CHAR(11),
-> name VARCHAR(50),
-> age INT,
-> gender VARCHAR(10)
-> );
运行结果:
Query OK, 0 rows affected (0.38 sec)
显示表
语法:SHOW TABLES;
输入语句:
mysql> SHOW TABLES;
运行结果:
+-----------------+
| Tables_in_mydb1 |
+-----------------+
| tb_stu |
+-----------------+
1 row in set (0.34 sec)
显示当前数据库中所有表的名字。
查询表
语法:DESC 表名;
输入语句:
mysql> DESC tb_stu;
运行结果:
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| number | char(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| gender | varchar(10) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.39 sec)
删除表
语法:DROP TABLE 表名;
输入语句:
mysql> DROP TABLE tb_stu;
运行结果:
Query OK, 0 rows affected (0.00 sec)
修改表
原表:
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| number | char(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| gender | varchar(10) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
1.添加列
语法:ALTER TABLE 表名
ADD(
列名 列类型
列名 列类型
··· ···
列名 列类型
);
输入语句:
mysql> ALTER TABLE tb_stu
-> ADD(
-> education VARCHAR(50)
-> );
运行结果:
Query OK, 0 rows affected (0.15 sec)
Records: 0 Duplicates: 0 Warnings: 0
查看当前表:
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| number | char(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| gender | varchar(10) | YES | | NULL | |
| education | varchar(50) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
2.修改列
语法:ALTER TABLE 表名
MODIFY 列名 列类型;
输入语句:
mysql> ALTER TABLE tb_stu
-> MODIFY education VARCHAR(100);
运行结果:
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
查看当前表:
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| number | char(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| gender | varchar(10) | YES | | NULL | |
| education | varchar(100) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
3.删除列
语法:ALTER TABLE 表名
DROP 列名;
输入语句:
mysql> ALTER TABLE tb_stu
-> DROP education;
运行结果:
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
查看当前表:
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| number | char(11) | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| gender | varchar(10) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4.修改表名
原表名:
+-----------------+
| Tables_in_mydb1 |
+-----------------+
| tb_stu |
+-----------------+
语法:ALTER TABLE 原表名
RENAME TO 新表名;
输入语句:
mysql> ALTER TABLE tb_stu
-> RENAME TO tb_student;
运行结果:
Query OK, 0 rows affected (0.01 sec)
新表名:
+-----------------+
| Tables_in_mydb1 |
+-----------------+
| tb_student |
+-----------------+
来源:CSDN
作者:葑鈊丶
链接:https://blog.csdn.net/qq_43733499/article/details/104357837