mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
6 rows in set (0.00 sec)
mysql> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
mysql> create database RichieSQL;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| richiesql |
| sakila |
| sys |
| world |
+--------------------+
7 rows in set (0.00 sec)
mysql> create database richiesql; # 已存在改名字的数据库,所以报错
ERROR 1007 (HY000): Can't create database 'richiesql'; database exists
mysql> create database if not exists richiesql; # if not exists意为如果不存在便创建,如果已存在也不会报错
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql> drop database richiesql; # 删除掉准备重新创建
Query OK, 0 rows affected (0.03 sec)
mysql> create database if not exists richiesql default charset utf8mb4; # default charset 意为指定字符集
Query OK, 1 row affected (0.04 sec)
mysql> use richiesql;
Database changed
mysql> select database();
+------------+
| database() |
+------------+
| richiesql |
+------------+
1 row in set (0.00 sec)
mysql> drop database richiesql;
Query OK, 0 rows affected (0.05 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sakila |
| sys |
| world |
+--------------------+
6 rows in set (0.00 sec)
mysql> drop database richiesql; # 删除不存在的数据库会报错
ERROR 1008 (HY000): Can't drop database 'richiesql'; database doesn't exist
mysql> drop database if exists richiesql; # if exists 意为如果存在便删除,如果不存在也不会报错
Query OK, 0 rows affected, 1 warning (0.03 sec)
mysql> create database richie; # 创建数据库
Query OK, 1 row affected (0.04 sec)
mysql> select database(); # 查询当前数据库
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
mysql> use richie; # 使用数据库
Database changed
mysql> show tables; # 查询当前数据库中的所有表,刚创建的新表,所以为空
Empty set (0.00 sec)
结构如下:
id | name | age | gender |
---|---|---|---|
1 | 张三 | 20 | 男 |
2 | 李四 | 21 | 女 |
3 | 王五 | 22 | 男 |
创建结构(不含内容):
mysql> create table user( # 此处user为表名
-> id int comment '编号', # 此处id为字段名,int为字段类型,comment可以为这个字段打备注
-> name varchar(50) comment '姓名', # 此处的50表示字符串最大50个字符
-> age int comment '年龄',
-> gender varchar(1) comment '性别'
-> ) comment '用户表'; # 这里的comment为整个表打备注
Query OK, 0 rows affected (0.03 sec)
mysql> show tables;
+------------------+
| Tables_in_richie |
+------------------+
| user |
+------------------+
1 row in set (0.00 sec)
mysql> desc user;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| age | int | YES | | NULL | |
| gender | varchar(1) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> show create table user;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| user | CREATE TABLE `user` (
`id` int DEFAULT NULL COMMENT '编号',
`name` varchar(50) DEFAULT NULL COMMENT '姓名',
`age` int DEFAULT NULL COMMENT '年龄',
`gender` varchar(1) DEFAULT NULL COMMENT '性别'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='用户表' |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.04 sec)
mysql> alter table user add nickname varchar(20) comment '昵称';
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(50) | YES | | NULL | |
| age | int | YES | | NULL | |
| gender | varchar(1) | YES | | NULL | |
| nickname | varchar(20) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> alter table user modify name varchar(20);
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| age | int | YES | | NULL | |
| gender | varchar(1) | YES | | NULL | |
| nickname | varchar(20) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> alter table user change name username varchar(10);
Query OK, 0 rows affected (0.10 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| username | varchar(10) | YES | | NULL | |
| age | int | YES | | NULL | |
| gender | varchar(1) | YES | | NULL | |
| nickname | varchar(20) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> alter table user drop nickname;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| username | varchar(10) | YES | | NULL | |
| age | int | YES | | NULL | |
| gender | varchar(1) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table user rename to emp;
Query OK, 0 rows affected (0.03 sec)
mysql> show tables;
+------------------+
| Tables_in_richie |
+------------------+
| emp |
+------------------+
1 row in set (0.00 sec)
mysql> drop table if exists user; # 删除了整张表,数据和表结构都删除了
Query OK, 0 rows affected, 1 warning (0.03 sec)
mysql> truncate table emp; # 删除了表,又重新创建了该表,数据删除了但表结构还在
Query OK, 0 rows affected (0.08 sec)
mysql> desc emp;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| username | varchar(10) | YES | | NULL | |
| age | int | YES | | NULL | |
| gender | varchar(1) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)