数据库基本操作
查看当前所有存在的数据库:SHOW DATABASES;
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
创建数据库:CREATE DATABASE database_name;
mysql> create database ronie; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | ronie | | sys | +--------------------+ 5 rows in set (0.00 sec)
使用数据库:USE database_name;
mysql> use ronie; Database changed mysql>
删除数据库:DROP DATABASE database_name;
mysql> drop database ronie; Query OK, 0 rows affected (0.01 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
数据库表的基本操作
创建数据表
查看数据库表:show tables;
mysql> show tables; +-----------------+ | Tables_in_ronie | +-----------------+ | user_info | +-----------------+ 1 row in set (0.00 sec)
创建数据表:CREATE TABLE name;
- 要创建的表的名称,不区分大小写,不能使用SQL语言中的关键字,如DROP、ALTER、INSERT等。
- 数据表中每一列(字段)的名称和数据类型,如果创建多列,就要用逗号隔开。
mysql> create table user_info -> ( -> id int(10), -> name varchar(25), -> age int(3) -> ); Query OK, 0 rows affected, 2 warnings (0.01 sec)
PS 查看警告
mysql> show warnings; +---------+------+------------------------------------------------------------------------------+ | Level | Code | Message | +---------+------+------------------------------------------------------------------------------+ | Warning | 1681 | Integer display width is deprecated and will be removed in a future release. | | Warning | 1681 | Integer display width is deprecated and will be removed in a future release. | +---------+------+------------------------------------------------------------------------------+ 2 rows in set (0.00 sec) mysql>
设置主键
- 主键,又称主码,是表中一列或多列的组合。主键约束(Primary Key Constraint)要求主键列的数据唯一,并且不允许为空。主键能够唯一地标识表中的一条记录,可以结合外键来定义不同数据表之间的关系,并且可以加快数据库查询的速度。主键和记录之间的关系如同身份证和人之间的关系,它们之间是一一对应的。主键分为两种类型:单字段主键和多字段联合主键。
单字段主键主键由一个字段组成,SQL语句格式分为以下两种情况。以下两个例子执行后的结果是一样的,都会在id字段上设置主键约束。
在定义列的同时指定主键,语法规则如下:
字段名 数据类型 PRIMARY KEY [默认值]
,重新定义table user_info:mysql> create table user_info -> ( -> id int(10) primary key, -> name varchar(25), -> age int(3) -> ); Query OK, 0 rows affected, 2 warnings (0.00 sec)
在定义完所有列之后指定主键:
[CONSTRAINT <约束名>] PRIMARY KEY [字段名]
,重新定义table user_info:mysql> create table user_info -> ( -> id int(10), -> name varchar(25), -> age int(3), -> primary key(id) -> ); Query OK, 0 rows affected, 2 warnings (0.01 sec)
多字段联合主键:
PRIMARY KEY [字段1, 字段2,. . ., 字段n]
mysql> create table user_info -> ( -> id int(10), -> name varchar(25), -> age int(3), -> primary key(id, name) -> ); Query OK, 0 rows affected, 2 warnings (0.01 sec)
使用外键约束
外键:- 外键用来在两个表的数据之间建立连接,可以是一列或者多列。一个表可以有一个或多个外键。外键对应的是参照完整性,一个表的外键可以为空值,若不为空值,则每一个外键值必须等于另一个表中主键的某个值。
- 外键:首先它是表中的一个字段,虽可以不是本表的主键,但要对应另外一个表的主键。外键的主要作用是保证数据引用的完整性,定义外键后,不允许删除在另一个表中具有关联关系的行。外键的作用是保持数据的一致性、完整性。
- 主表(父表):对于两个具有关联关系的表而言,相关联字段中主键所在的那个表即是主表。从表(子表):对于两个具有关联关系的表而言,相关联字段中外键所在的那个表即是从表。
创建外键的语法:
[CONSTRAINT <外键名>] FOREIGN KEY 字段名1 [ ,字段名2,…] REFERENCES <主表名> 主键列1 [ ,主键列2,…]
mysql> create table department -> ( -> id int(10), -> department_name varchar(25), -> location varchar(20), -> primary key(id) -> ); Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> drop table user_info; Query OK, 0 rows affected (0.00 sec) mysql> create table user_info -> ( -> id int(10), -> name varchar(25), -> department_id int(10), -> age int(3), -> constraint fk_user_department foreign key(department_id) references department(id) -> ); Query OK, 0 rows affected, 3 warnings (0.02 sec) mysql> show warnings; +---------+------+------------------------------------------------------------------------------+ | Level | Code | Message | +---------+------+------------------------------------------------------------------------------+ | Warning | 1681 | Integer display width is deprecated and will be removed in a future release. | | Warning | 1681 | Integer display width is deprecated and will be removed in a future release. | | Warning | 1681 | Integer display width is deprecated and will be removed in a future release. | +---------+------+------------------------------------------------------------------------------+ 3 rows in set (0.00 sec) mysql>
使用非空约束
非空约束(Not Null Constraint)指字段的值不能为空。对于使用了非空约束的字段,如果用户在添加数据时没有指定值,数据库系统会报错。语法规则:字段名 数据类型 not null
。mysql> create table user -> ( -> id int(10) not null, -> name varchar(250) not null, -> password varchar(250) not null, -> age int(3) -> ); Query OK, 0 rows affected, 2 warnings (0.01 sec)
使用唯一性约束
唯一性约束(Unique Constraint)要求该列唯一,允许为空,但只能出现一个空值。唯一约束可以确保一列或者几列不出现重复值。在定义完列之后直接指定唯一约束,语法规则如下:字段名 数据类型 UNIQUE
。一个表中可以有多个字段声明为UNIQUE,但只能有一个PRIMARY KEY声明;声明为PRIMAY KEY的列不允许有空值,但是声明为UNIQUE的字段允许空值(NULL)的存在。mysql> create table user -> ( -> id int(10) not null unique, -> name varchar(250) not null, -> password varchar(250) not null, -> age int(3) -> ); Query OK, 0 rows affected, 2 warnings (0.01 sec)
使用默认约束
默认约束(Default Constraint)指定某列的默认值。如果插入一条新的记录时没有为这个字段赋值,那么系统会自动为这个字段赋值。mysql> drop table user; Query OK, 0 rows affected (0.01 sec) mysql> create table user -> ( -> id int(10) not null unique, -> name varchar(250) not null, -> password varchar(250) default 123456, -> age int(3) -> ); Query OK, 0 rows affected, 2 warnings (0.01 sec)
设置表的属性值自动增加
在数据库应用中,我们可能希望在每次插入新记录时,系统自动生成字段的主键值。可以通过为表主键添加AUTO_INCREMENT关键字来实现。默认的,在MySQL中AUTO_INCREMENT的初始值是1,每新增一条记录,字段值自动加1。一个表只能有一个字段使用AUTO_INCREMENT约束,且该字段必须为主键的一部分。AUTO_INCREMENT约束的字段可以是任何整数类型(TINYINT、SMALLIN、INT、BIGINT等)。mysql> create table user -> ( -> id int(10) primary key auto_increment, -> name varchar(250) not null, -> password varchar(250) default 123456, -> age int(3) -> ); Query OK, 0 rows affected, 2 warnings (0.02 sec)
上述数据表创建后。表中的id字段的值在添加记录的时候会自动增加,在插入记录的时候,默认的自增字段id的值从1开始,每次添加一条新记录,该值自动加1。
mysql> insert into user (name, age) values ('ronie', 18), ('kelly', 20), ('jack', 24); Query OK, 3 rows affected (0.00 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql> select * from user; +----+-------+----------+------+ | id | name | password | age | +----+-------+----------+------+ | 1 | ronie | 123456 | 18 | | 2 | kelly | 123456 | 20 | | 3 | jack | 123456 | 24 | +----+-------+----------+------+ 3 rows in set (0.00 sec) mysql>
查看数据表结构
DESCRIBE/DESC语句可以查看表的字段信息,其中包括字段名、字段数据类型、是否为主键、是否有默认值等。语法规则如下:
DESCRIBE 表名
;DESC 表名
;mysql> describe user; +----------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | name | varchar(250) | NO | | NULL | | | password | varchar(250) | YES | | 123456 | | | age | int | YES | | NULL | | +----------+--------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec) mysql> desc user; +----------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+----------------+ | id | int | NO | PRI | NULL | auto_increment | | name | varchar(250) | NO | | NULL | | | password | varchar(250) | YES | | 123456 | | | age | int | YES | | NULL | | +----------+--------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec) mysql>
- NULL:表示该列是否可以存储NULL值。
- Key:表示该列是否已编制索引。
- PRI表示该列是表主键的一部分;
- UNI表示该列是UNIQUE索引的一部分;
- MUL表示在列中某个给定值允许出现多次。
- Default:表示该列是否有默认值,有的话指定值是多少。
- Extra:表示可以获取的与给定列有关的附加信息,例如AUTO_INCREMENT等。
查看表详细结构语句SHOW CREATE TABLE
mysql> show create user; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 mysql> show create table user; +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | user | CREATE TABLE `user` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(250) NOT NULL, `password` varchar(250) DEFAULT '123456', `age` int DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci | +-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql>
mysql> show create table user\G; *************************** 1. row *************************** Table: user Create Table: CREATE TABLE `user` ( `id` int NOT NULL AUTO_INCREMENT, `name` varchar(250) NOT NULL, `password` varchar(250) DEFAULT '123456', `age` int DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci 1 row in set (0.00 sec) ERROR: No query specified mysql>
修改数据表
修改表名,MySQL是通过ALTER TABLE语句来实现表名的修改的,
ALTER TABLE <旧表名> RENAME [TO] <新表名>;
mysql> show tables; +-----------------+ | Tables_in_ronie | +-----------------+ | department | | user | | user_info | +-----------------+ 3 rows in set (0.00 sec) mysql> alter table user rename user_login_info; Query OK, 0 rows affected (0.01 sec) mysql> show tables; +-----------------+ | Tables_in_ronie | +-----------------+ | department | | user_info | | user_login_info | +-----------------+ 3 rows in set (0.00 sec) mysql>
修改字段的数据类型
修改字段的数据类型,就是把字段的数据类型转换成另一种数据类型。语法:ALTER TABLE <表名> MODIFY <字段名> <数据类型>
。其中,“表名”指要修改数据类型的字段所在表的名称,“字段名”指需要修改的字段,“数据类型”指修改后字段的新数据类型。mysql> show tables; +-----------------+ | Tables_in_ronie | +-----------------+ | department | | user_info | | user_login_info | +-----------------+ 3 rows in set (0.00 sec) mysql> desc user_info; +---------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+-------------+------+-----+---------+-------+ | id | int | YES | | NULL | | | name | varchar(25) | YES | | NULL | | | department_id | int | YES | MUL | NULL | | | age | int | YES | | NULL | | +---------------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql> alter table user_info modify name varchar(250); Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc user_info; +---------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+--------------+------+-----+---------+-------+ | id | int | YES | | NULL | | | name | varchar(250) | YES | | NULL | | | department_id | int | YES | MUL | NULL | | | age | int | YES | | NULL | | +---------------+--------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql>
修改字段名
MySQL中修改表字段名的语法规则如下:ALTER TABLE <表名> CHANGE <旧字段名> <新字段名> <新数据类型>;
。其中,“旧字段名”指修改前的字段名;“新字段名”指修改后的字段名;“新数据类型”指修改后的数据类型,如果不需要修改字段的数据类型,将新数据类型设置成与原来一样即可,但数据类型不能为空。mysql> show tables; +-----------------+ | Tables_in_ronie | +-----------------+ | department | | user_info | | user_login_info | +-----------------+ 3 rows in set (0.00 sec) mysql> desc user_info; +---------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+--------------+------+-----+---------+-------+ | id | int | YES | | NULL | | | name | varchar(250) | YES | | NULL | | | department_id | int | YES | MUL | NULL | | | age | int | YES | | NULL | | +---------------+--------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql> alter table user_info change name username varchar(200); Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc user_info; +---------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+--------------+------+-----+---------+-------+ | id | int | YES | | NULL | | | username | varchar(200) | YES | | NULL | | | department_id | int | YES | MUL | NULL | | | age | int | YES | | NULL | | +---------------+--------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql>
添加字段
语法:ALTER TABLE <表名> ADD <新字段名> <数据结构> [约束条件] [FIRST | AFTER 已存在字段名]
。一个完整字段包括字段名、数据类型、完整性约束。新字段名为需要添加的字段的名称;FIRST为可选参数,其作用是将新添加的字段设置为表的第一个字段;AFTER为可选参数,其作用是将新添加的字段添加到指定的“已存在字段名”的后面。添加无完整性约束条件的字段
mysql> show tables; +-----------------+ | Tables_in_ronie | +-----------------+ | department | | user_info | | user_login_info | +-----------------+ 3 rows in set (0.00 sec) mysql> desc department; +-----------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------+-------------+------+-----+---------+-------+ | id | int | NO | PRI | NULL | | | department_name | varchar(25) | YES | | NULL | | | location | varchar(20) | YES | | NULL | | +-----------------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> alter table department add avg_salary float after department_name; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc department; +-----------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------+-------------+------+-----+---------+-------+ | id | int | NO | PRI | NULL | | | department_name | varchar(25) | YES | | NULL | | | avg_salary | float | YES | | NULL | | | location | varchar(20) | YES | | NULL | | +-----------------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql>
添加有完整性约束条件的字段
mysql> desc department; +-----------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------+-------------+------+-----+---------+-------+ | id | int | NO | PRI | NULL | | | department_name | varchar(25) | YES | | NULL | | | avg_salary | float | YES | | NULL | | | location | varchar(20) | YES | | NULL | | +-----------------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql> alter table department add num_of_people int(3) default 0 after avg_salary; Query OK, 0 rows affected, 1 warning (0.01 sec) Records: 0 Duplicates: 0 Warnings: 1 mysql> desc department; +-----------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------+-------------+------+-----+---------+-------+ | id | int | NO | PRI | NULL | | | department_name | varchar(25) | YES | | NULL | | | avg_salary | float | YES | | NULL | | | num_of_people | int | YES | | 0 | | | location | varchar(20) | YES | | NULL | | +-----------------+-------------+------+-----+---------+-------+ 5 rows in set (0.00 sec) mysql>
删除字段
删除字段是将数据表中的某个字段从表中移除:ALTER TABLE <表名> DROP <字段名>;
。mysql> desc department; +-----------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------+-------------+------+-----+---------+-------+ | id | int | NO | PRI | NULL | | | department_name | varchar(25) | YES | | NULL | | | avg_salary | float | YES | | NULL | | | num_of_people | int | YES | | 0 | | | location | varchar(20) | YES | | NULL | | +-----------------+-------------+------+-----+---------+-------+ 5 rows in set (0.00 sec) mysql> alter table department drop avg_salary; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc department; +-----------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------+-------------+------+-----+---------+-------+ | id | int | NO | PRI | NULL | | | department_name | varchar(25) | YES | | NULL | | | num_of_people | int | YES | | 0 | | | location | varchar(20) | YES | | NULL | | +-----------------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql>
修改字段的排列位置
对于一个数据表来说,在创建的时候,字段在表中的排列顺序就已经确定了,但表的结构并不是完全不可以改变的,可以通过ALTER TABLE来改变表中字段的相对位置。语法格式如下:ALTER TABLE <表名> MODIFY <字段1> <数据类型> FIRST|AFTER <字段2>;
。mysql> desc department; +-----------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------+-------------+------+-----+---------+-------+ | id | int | NO | PRI | NULL | | | department_name | varchar(25) | YES | | NULL | | | num_of_people | int | YES | | 0 | | | location | varchar(20) | YES | | NULL | | +-----------------+-------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql> alter table department modify department_name varchar(250) after num_of_people; Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc department; +-----------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------+--------------+------+-----+---------+-------+ | id | int | NO | PRI | NULL | | | num_of_people | int | YES | | 0 | | | department_name | varchar(250) | YES | | NULL | | | location | varchar(20) | YES | | NULL | | +-----------------+--------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql>
更改表的存储引擎
我们可以根据自己的需要,选择不同的引擎,甚至可以为每一张表选择不同的存储引擎。MySQL中主要的存储引擎有MyISAM、InnoDB、MEMORY(HEAP)、BDB、FEDERATED等。可以使用SHOW ENGINES;语句查看系统支持的存储引擎。更改储存引擎的语法:ALTER TABLE <表名> ENGINE=<更改后的存储引擎名>;
。mysql> show engines; +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | Engine | Support | Comment | Transactions | XA | Savepoints | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | ARCHIVE | YES | Archive storage engine | NO | NO | NO | | BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO | | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO | | FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL | | MyISAM | YES | MyISAM storage engine | NO | NO | NO | | PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO | | InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES | | MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO | | CSV | YES | CSV storage engine | NO | NO | NO | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ 9 rows in set (0.00 sec) mysql> alter table user_login_info engine=memory; Query OK, 3 rows affected (0.02 sec) Records: 3 Duplicates: 0 Warnings: 0 mysql>
删除表的外键约束
对于数据库中定义的外键,如果不再需要,可以将其删除。外键一旦删除,就会解除主表和从表间的关联关系。语法:ALTER TABLE <表名> DROP FOREIGN KEY <外键约束名>
mysql> desc user_info; +---------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------------+--------------+------+-----+---------+-------+ | id | int | YES | | NULL | | | username | varchar(200) | YES | | NULL | | | department_id | int | YES | MUL | NULL | | | age | int | YES | | NULL | | +---------------+--------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql> alter table user_info drop foreign key fk_user_department; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table user_info drop foreign key fk_user_department; ERROR 1091 (42000): Can't DROP 'fk_user_department'; check that column/key exists
删除数据表
删除没有被关联的表
语法:DROP TABLE [IF EXISTS]表1, 表2,…表n;
。mysql> show tables; +-----------------+ | Tables_in_ronie | +-----------------+ | department | | tmp | | user_info | | user_login_info | +-----------------+ 4 rows in set (0.00 sec) mysql> drop table if exists tmp; Query OK, 0 rows affected (0.00 sec) mysql> show tables; +-----------------+ | Tables_in_ronie | +-----------------+ | department | | user_info | | user_login_info | +-----------------+ 3 rows in set (0.01 sec)
删除被其他表关联的主表
在数据表之间存在外键关联的情况下,如果直接删除父表,结果会显示失败,原因是直接删除将破坏表的参照完整性。如果必须要删除,可以先删除与它关联的子表,再删除父表,只是这样就同时删除了两个表中的数据。有的情况下可能要保留子表,这时若要单独删除父表,只需将关联的表的外键约束条件取消,然后就可以删除父表。mysql> create table main -> ( -> id int(10) primary key, -> main_name varchar(25) -> ); Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> create table follow -> ( -> id int(10) primary key, -> follow_name varchar(25), -> main_id int(10), -> constraint fk_follow_main foreign key (main_id) references main (id) -> ); Query OK, 0 rows affected, 2 warnings (0.02 sec) mysql> show tables; +-----------------+ | Tables_in_ronie | +-----------------+ | department | | follow | | main | | user_info | | user_login_info | +-----------------+ 5 rows in set (0.00 sec) mysql> drop main; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'main' at line 1 mysql> show create table main \G; *************************** 1. row *************************** Table: main Create Table: CREATE TABLE `main` ( `id` int NOT NULL, `main_name` varchar(25) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci 1 row in set (0.00 sec) ERROR: No query specified mysql> show create table follow \G; *************************** 1. row *************************** Table: follow Create Table: CREATE TABLE `follow` ( `id` int NOT NULL, `follow_name` varchar(25) DEFAULT NULL, `main_id` int DEFAULT NULL, PRIMARY KEY (`id`), KEY `fk_follow_main` (`main_id`), CONSTRAINT `fk_follow_main` FOREIGN KEY (`main_id`) REFERENCES `main` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci 1 row in set (0.00 sec) ERROR: No query specified mysql> drop main; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'main' at line 1 mysql> drop table main; Query OK, 0 rows affected (0.00 sec) mysql> drop table follow; Query OK, 0 rows affected (0.01 sec)
来学习啦!