MySQL Data Manipulation
Outline:
- SELECT
- ORDER BY
- ...
Mysql语法简介
MYSQL DATA MANIPULATION
SELECT
1 | SELECT select_list |
;
是可选的,表明一条语句的结束- SQL是大小写不敏感的,因此
SELECT0
,FROM
这些关键字可大写可小写 SELECT
的结果称为一个结果集- 如果SELECT的参数是空行(连NULL都没有),会返回NULL。
### Returning n Random Records from a Table
1 | select ename, job |
Transforming Nulls into Real Values
Use the function COALESCE to substitute real values for nulls:
1 | select coalesce(comm, 0) |
COALESCE 接受至少一个参数,返回第一个非空参数。 如果comm为null,则返回0
或者:
1 | select case |
ORDER BY
默认是ASC
1 | SELECT |
ORDER BY的参数一般是字段,当然也可以是列号, 但后一种方式不推荐
1 | SELECT |
WHERE
1 | SELECT |
The search_condition
is a combination of one or more expressions using the logical operator AND
, OR
and NOT
.
In MySQL, a predicate is a Boolean expression that evaluates to TRUE
, FALSE
, or UNKNOWN
.
The SELECT
statement will include any row that satisfies the search_condition
in the result set.
Besides the SELECT
statement, you can use the WHERE
clause in the UPDATE
or DELETE
statement to specify which rows to update or delete.
When executing a SELECT
statement with a WHERE
clause, MySQL evaluates the WHERE
clause after the FROM
clause and before the SELECT
and ORDER BY
clauses:
BETWEEN
expression BETWEEN low AND high
LIKE
The LIKE
operator evaluates to TRUE
if a value matches a specified pattern.
To form a pattern, you use the %
and _
wildcards.
- The
%
wildcard matches any string of zero or more characters - the
_
wildcard matches any single character.
The following query finds the employees whose last names end with the string 'son'
:
1 | SELECT |
IN
The IN
operator returns TRUE
if a value matches any value in a list.
1 | value IN (value1, value2,...) |
The following example uses the WHERE
clause with the IN
operator to find employees who locate in the office with office code 1.
1 | SELECT |
IS NULL
To check if a value is NULL
or not, you use the IS NULL
operator, not the equal operator (=
). The IS NULL
operator returns TRUE
if a value is NULL
.
1 | value IS NULLCode language: SQL (Structured Query Language) (sql) |
In the database world,
NULL
is a marker that indicates that a value is missing or unknown. And NULL is not equivalent to the number 0 or an empty string.
对应的,还有IS NOT NULL
LIMIT
注意: limit
作用于结果集产生的最后,在order by
之后,所以要先order by
, 再 lmit
The LIMIT
clause is used in the SELECT
statement to constrain the number of rows to return.
1 | SELECT |
The
offset
specifies the offset of the first row to return. OFFSET从0开始计数,缺省也为0The
row_count
specifies the maximum number of rows to return.LIMIT
子句对于超过其范围的行,会返回空行而不是NULL
:1
2
3
4
5
6
7select salary from `Employee` limit 1 offset 1
# 如果Employee只有一行,则该查询返回空行:
+--------+
| salary |
+--------+
+--------+
# 不会返回null
可以和ORDER BY
结合:
1 | SELECT |
示例: get the highest or lowest rows
1 | SELECT |
- First, the
ORDER BY
clause sorts the customers by credits in high to low. - Then, the
LIMIT
clause returns the first 5 rows.
IS NULL
To test whether a value is NULL
or not, you use the IS NULL
operator. Here’s the basic syntax of the IS NULL
operator:
1 | value IS NULLCode language: SQL (Structured Query Language) (sql) |
If the value is NULL
, the expression returns true. Otherwise, it returns false.
Note that MySQL does not have a built-in BOOLEAN
type. It uses the TINYINT(1)
to represent the BOOLEAN
values i.e., true means 1 and false means 0.
Because the IS NULL
is a comparison operator, you can use it anywhere that an operator can be used e.g., in the SELECT
or WHERE
clause.
See the following example:
1 | SELECT 1 IS NULL, -- 0 |
To check if a value is not NULL
, you use IS NOT NULL
operator:
1 | value IS NOT NULLCode language: SQL (Structured Query Language) (sql) |
This expression returns true (1) if the value is not NULL
. Otherwise, it returns false (0).
Consider the following example:
1 | SELECT 1 IS NOT NULL, -- 1 |
SELECT DISTINCT
1 | SELECT DISTINCT |
DISTINCT
: 从选出的行中选择唯一的- 如果
DISTINCT
后面跟多个字段,就会将这些字段作为一个整体来判断唯一性 - MYSQL认为所有NULL都是相等的,因此多个NULL在DISTINCT后只剩下一个
Alias
在一次查询中重复引用一个表名会引发ERROR, 因此要定义别名
列别名:
1 | SELECT |
表达式别名:
1 | SELECT |
表别名:
1 | table_name [AS] table_alias |
JOIN
A join is a method of linking data between one (self-join) or more tables based on values of the common column between the tables.
MySQL supports the following types of joins:
- Inner join
- Left join
- Right join
- Cross join
To join tables, you use the cross join, inner join, left join, or right join clause. The join clause is used in the SELECT
statement appeared after the FROM
clause.
Note that MySQL hasn’t supported the FULL OUTER JOIN
yet.
Join会创建一个包含原本两方所有字段的新行,并加入结果集
INNER JOIN
1 | SELECT column_list |
The inner join clause compares each row from the first table with every row from the second table.
将左边的每一行与右边的所有行匹配
只有两边的行同时满足,才会被join
如果两张表中作为条件来比较的字段相同,那么可以用USING
来替代ON
:
1 | using(id) |
LEFT JOIN
对于左边的一行,如果右边的行都不满足条件,依然会创建出新行,只不过新行中,原来的右边的行的字段的对应值为NULL
1 | SELECT |
RIGHT JOIN
从右边的行中选择,将右边的每一行与左边的所有行匹配。 如果左边的所有行都不满足条件,那么依然创建出新行,新行中左边行的字段值为NULL
1 | SELECT column_list |
CROSS JOIN
没有查询条件,产生笛卡尔积
1 | SELECT select_list |
Self Join
一个表和自己做join, 因为同一次查询中不能重复引用同一张表,因此必须定义别名
示例:
1 | SELECT |
GROUP BY
The GROUP BY
clause returns one row for each group.
可以和聚合函数结合。 不结合聚合函数的GROUP BY
和SELECT DISTINCT
是类似的
MYSQL8.0后不会对GROUP BY
的结果集进行排序
The GROUP BY
clause is often used with an aggregate function to perform calculations and return a single value for each subgroup.
1 | SELECT |
可以按多个字段group by
,用逗号分隔:
1 | select |
HAVING
The HAVING
clause is often used with the GROUP BY
clause to filter groups based on a specified condition. If you omit the GROUP BY
clause, the HAVING
clause behaves like the WHERE
clause.
1 | SELECT |
ROLLUP
Subquery
子查询必须用括号括起来, 可以作为表达式,用在任何需要表达式的地方
子查询如果用在FROM
子句,则返回的结果集是一张临时表,被称为“导出表”, 导出表一定要有别名, 好作为一张表被引用
WHERE
子查询用在WHERE
子句, 一般是和聚合函数的结合, 将聚合函数的结果返回, 然后在WHERE中进行条件运算
:
1 | SELECT |
IN & NOT IN
如果子查询返回不止一个结果(即不止一行), 那还可以用来作为[NOT] IN
的参数,在WHERE
子句中进行条件运算
1 | SELECT |
FROM
子查询作为派生表,必须要有别名
1 | SELECT |
correlated subquery
子查询可以是独立的,一共只执行一次;也可以是关联的, 外层查询的每处理一行就执行一次:
1 | SELECT |
EXISTS & NOT EXISTS
子查询就是表达式,因此用在[NOT] EXISTS
子句中时,子查询只会返回一个BOOL值:
- 只要子查询返回至少一行,就返回TRUE,否则返回FALSE
- 一般而言,作为
EXISTS
参数的子查询都是关联查询,这样才能和外层查询联系起来, 否则如果是独立查询, 每次计算时都返回固定的BOOL值,没什么意义
The following query finds sales orders whose total values are greater than 60K.
1 | SELECT |
It returns 3 rows, meaning that there are three sales orders whose total values are greater than 60K.
You can use the query above as a correlated subquery to find customers who placed at least one sales order with the total value greater than 60K by using the EXISTS
operator:
1 | SELECT |
Derived Tables
派生表就是用在SELECT
子句的子查询, 该子查询返回一张表(属于临时表), 并一定具有别名以被引用
例子
Suppose you have to classify the customers who bought products in 2003 into 3 groups: platinum
, gold
, and silver
. And you need to know the number of customers in each group with the following conditions:
- Platinum customers who have orders with the volume greater than 100K.
- Gold customers who have orders with the volume between 10K and 100K.
- Silver customers who have orders with the volume less than 10K.
To form this query, you first need to put each customer into the respective group using CASE
expression and GROUP BY
clause as follows:
1 | SELECT |
The following is the output of the query:
Then, you can use this query as the derived table and perform grouping as follows:
1 | SELECT |
The query returns the customer groups and the number of customers in each.
EXISTS
The EXISTS
operator is a Boolean operator that returns either true or false. The EXISTS
operator is often used to test for the existence of rows returned by the subquery.
The following illustrates the basic syntax of the EXISTS
operator:
1 | SELECT |
If the subquery returns at least one row, the EXISTS
operator returns true, otherwise, it returns false.
In addition, the EXISTS
operator terminates further processing immediately once it finds a matching row, which can help improve the performance of the query.
The NOT
operator negates the EXISTS
operator. In other words, the NOT EXISTS
returns true if the subquery returns no row, otherwise it returns false.
Note that you can use SELECT *
, SELECT column
, SELECT a_constant
, or anything in the subquery. The results are the same because MySQL ignores the select list appeared in the SELECT
clause.
UNION
1 | SELECT column_list |
UNION
的参数也是一张表(由SELECT
子句产生),但不是派生表,因此不需要别名
UNION
将两张表垂直地并起来, 而JOIN
将两张表水平地并起来:
要将多张表UNION
起来,作为UNION
主语的SELECT和宾语的SELECT子句的对应字段的数量和顺序必须相同, 类型也必须相容
比如:
1 | SELECT |
UNION
默认是UNION DISTINCT
; 要保留重复的行,需要使用UNION ALL
UNION
后产生的大表的字段名用的是UNION
主语的SELECT
的字段名使用
ORDER BY
对产生的大表的行进行排序- 注意, UNION宾语的那张子表里不需要排序
1
2
3
4
5
6
7
8
9SELECT
concat(firstName,' ',lastName) fullname
FROM
employees
UNION SELECT
concat(contactFirstName,' ',contactLastName)
FROM
customers
ORDER BY fullname;
MINUS
1 | SELECT select_list1 |
Unfortunately, MySQL does not support MINUS
operator. However, you can use join to emulate it.
To emulate the MINUS
of two queries, you use the following syntax:
1 | SELECT |
INSERT
1 | INSERT INTO table(c1,c2,...) |
- The number of columns and values must be the same. In addition, the positions of columns must be corresponding with the positions of their values
To insert multiple rows into a table using a single INSERT
statement, you use the following syntax:
1 | INSERT INTO table(c1,c2,...) |
insert using default value
If you want to insert a default value into a column, you have two ways:
- Ignore both the column name and value in the
INSERT
statement. - Specify the column name in the
INSERT INTO
clause and use theDEFAULT
keyword in theVALUES
clause.
The following example demonstrates the second way:
1 | INSERT INTO tasks(title,priority) |
In this example, we specified the priority
column and the DEFAULT
keyword.
Because the default value for the column priority
is 3 as declared in the table definition:
1 | priority TINYINT NOT NULL DEFAULT 3Code language: SQL (Structured Query Language) (sql) |
MySQL uses the number 3 to insert into the priority
column.
Inserting dates
To insert a literal date value into a column, you use the following format:
1 | 'YYYY-MM-DD'Code language: SQL (Structured Query Language) (sql) |
In this format:
YYYY
represents a four-digit year e.g., 2018.MM
represents a two-digit month e.g., 01, 02, and 12.DD
represents a two-digit day e.g., 01, 02, 30.
The following statement inserts a new row to the tasks
table with the start and due date values:
1 | INSERT INTO tasks(title, start_date, due_date) |
The following picture shows the contents of the tasks
table after the insert:
It is possible to use expressions in the VALUES
clause. For example, the following statement adds a new task using the current date for start date and due date columns:
1 | INSERT INTO tasks(title,start_date,due_date) |
In this example, we used the CURRENT_DATE()
function as the values for the start_date
and due_date
columns. Note that the CURRENT_DATE()
function is a date function that returns the current system date.
Insert Into Select
In the previous tutorial, you learned how to insert one or more rows into a table using the INSERT
statement with a list of column values specified in the VALUES
clause.
1 | INSERT INTO table_name(c1,c2,...) |
Besides using row values in the VALUES
clause, you can use the result of a SELECT
statement as the data source for the INSERT
statement.
The following illustrates the syntax of the INSERT INTO SELECT
statement:
1 | INSERT INTO table_name(column_list) |
In this syntax, instead of using the VALUES
clause, you can use a SELECT
statement. The SELECT
statement can retrieve data from one or more tables.
The INSERT INTO SELECT
statement is very useful when you want to copy data from other tables to a table or to summary data from multiple tables into a table.
UPDATE
1 | UPDATE salary |
CASE
CASE
返回一个字段值
case和when的参数字段都不要加别名, 别名要加在整个case子句后面,也就是END
后面
Simple CASE expression
1 | CASE case_value |
- 如果没有任何一个
WHEN
子句被匹配,则进入ELSE
子句。ELSE
子句是可选的, 如果省略了ELSE
子句,则不匹配任何when时,直接返回NULL
例子:
1 | SELECT |
Searched CASE expression
1 | CASE |
例子:
1 | SELECT |
DERIVED TABLE
派生表是从SELECT语句返回的虚拟表。派生表类似于临时表,但是在SELECT
语句中使用派生表比临时表简单得多,因为它不需要创建临时表的步骤。
术语:*派生表*和子查询通常可互换使用。当SELECT
语句的FROM
子句中使用独立子查询时,我们将其称为派生表。
以下说明了使用派生表的查询:
请注意,独立子查询是一个子查询,可独立于包含该语句的执行语句。
与子查询不同,派生表必须具有别名,以便稍后在查询中引用其名称。 如果派生表没有别名,MySQL将发出以下错误:
1 | Every derived table must have its own alias. |
以下说明了使用派生表的SQL语句:
1 | SELECT |
aA12345678
FUNCTION
ROUND
ROUND(X,D)
此函数返回x舍入到最接近的整数。如果第二个参数,D有提供,则函数返回x四舍五入至第D位小数点。D必须是正数
IFNULL
MySQL IFNULL
函数是MySQL控制流函数之一,它接受两个参数,如果不是NULL
,则返回第一个参数。 否则,IFNULL
函数返回第二个参数。
两个参数可以是文字值或表达式。
以下说明了IFNULL
函数的语法:
1 | IFNULL(expression_1,expression_2); |
如果expression_1
不为NULL
,则IFNULL
函数返回expression_1
; 否则返回expression_2
的结果。
IFNULL
函数根据使用的上下文返回字符串或数字。
如果要返回基于TRUE
或FALSE
条件的值,而不是NULL
,则应使用IF函数。
Aggregate Functions
1 | function_name(DISTINCT | ALL expression) |
- use
DISTINCT
if you want to calculate based on distinct values orALL
in case you want to calculate all values including duplicates. The default isALL
- The aggregate functions are often used with the
GROUP BY
clause to calculate an aggregate value for each group
AVG()
The AVG()
function calculates the average value of a set of values. It ignores NULL in the calculation.
1 | AVG(expression) |
COUNT()
The COUNT()
function returns the number of the value in a set.
For example, you can use the COUNT()
function to get the number of products in the products
table as shown in the following query:
1 | SELECT |
SUM()
The SUM()
function returns the sum of values in a set. The SUM()
function ignores NULL
. If no matching row found, the SUM()
function returns NULL.
To get the total order value of each product, you can use the SUM()
function in conjunction with the GROUP BY
clause as follows:
1 | SELECT |
MAX()
The MAX()
function returns the maximum value in a set.
1 | MAX(expression)Code language: SQL (Structured Query Language) (sql) |
For example, you can use the MAX()
function to get the highest buy price from the products
table as shown in the following query:
1 | SELECT |
CONSTRAINTS
Primary Key
When you define a primary key for a table, MySQL automatically creates an index called PRIMARY
1) Define a PRIMARY KEY
constraint in CREATE TABLE
Typically, you define the primary key for a table in the CREATE TABLE
statement.
If the primary key has one column, you can use the PRIMARY KEY
constraint as a column constraint:
1 | CREATE TABLE table_name( |
When the primary key has more than one column, you must use the PRIMARY KEY
constraint as a table constraint.
1 | CREATE TABLE table_name( |
In this syntax, you separate columns in the column_list
by commas (,).
The PRIMARY KEY
table constraint can be used when the primary key has one column:
1 | CREATE TABLE table_name ( |
Foreign Key
A foreign key is a column or group of columns in a table that links to a column or group of columns in another table. The foreign key places constraints on data in the related tables, which allows MySQL to maintain referential integrity.
Once a foreign key constraint is in place, the foreign key columns from the child table must have the corresponding row in the parent key columns of the parent table or values in these foreign key column must be NULL
(see the SET NULL
action example below).
Self-referencing foreign key
Sometimes, the child and parent tables may refer to the same table. In this case, the foreign key references back to the primary key within the same table.
See the following employees
table from the sample database.
The reportTo
column is a foreign key that refers to the employeeNumber
column which is the primary key of the employees
table.
This relationship allows the employees
table to store the reporting structure between employees and managers. Each employee reports to zero or one employee and an employee can have zero or many subordinates.
The foreign key on the column reportTo
is known as a recursive or self-referencing foreign key.
MySQL FOREIGN KEY
syntax
Here is the basic syntax of defining a foreign key constraint in the CREATE TABLE
or ALTER TABLE
statement:
1 | [CONSTRAINT constraint_name] |
In this syntax:
First, specify the name of foreign key constraint that you want to create after the CONSTRAINT
keyword. If you omit the constraint name, MySQL automatically generates a name for the foreign key constraint.
Second, specify a list of comma-separated foreign key columns after the FOREIGN KEY
keywords. The foreign key name is also optional and is generated automatically if you skip it.
Third, specify the parent table followed by a list of comma-separated columns to which the foreign key columns reference.
Finally, specify how foreign key maintains the referential integrity between the child and parent tables by using the ON DELETE
and ON UPDATE
clauses. The reference_option
determines action which MySQL will take when values in the parent key columns are deleted (ON DELETE
) or updated (ON UPDATE
).
MySQL has five reference options: CASCADE
, SET NULL
, NO ACTION
, RESTRICT
, and SET DEFAULT
.
CASCADE
: if a row from the parent table is deleted or updated, the values of the matching rows in the child table automatically deleted or updated.SET NULL
: if a row from the parent table is deleted or updated, the values of the foreign key column (or columns) in the child table are set toNULL
.RESTRICT
: if a row from the parent table has a matching row in the child table, MySQL rejects deleting or updating rows in the parent table.NO ACTION
: is the same asRESTRICT
.SET DEFAULT
: is recognized by the MySQL parser. However, this action is rejected by both InnoDB and NDB tables.
In fact, MySQL fully supports three actions: RESTRICT
, CASCADE
and SET NULL
.
If you don’t specify the ON DELETE
and ON UPDATE
clause, the default action is RESTRICT
.
MySQL FOREIGN KEY
examples
Let’s create a new database called fkdemo
for the demonstration.
1 | CREATE DATABASE fkdemo; |
RESTRICT
& NO ACTION
actions
Inside the fkdemo
database, create two tables categories
and products
:
1 | CREATE TABLE categories( |
The categoryId
in the products
table is the foreign key column that refers to the categoryId
column in the categories
table.
Because we don’t specify any ON UPDATE
and ON DELETE
clauses, the default action is RESTRICT
for both update and delete operation.
The following steps illustrate the RESTRICT
action.
\1) Insert two rows into the categories
table:
1 | INSERT INTO categories(categoryName) |
\2) Select data from the categories
table:
1 | SELECT * FROM categories;Code language: SQL (Structured Query Language) (sql) |
\3) Insert a new row into the products
table:
1 | INSERT INTO products(productName, categoryId) |
It works because the categoryId
1 exists in the categories
table.
\4) Attempt to insert a new row into the products
table with a categoryId
value does not exist in the categories
table:
1 | INSERT INTO products(productName, categoryId) |
MySQL issued the following error:
1 | Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)Code language: JavaScript (javascript) |
\5) Update the value in the categoryId
column in the categories
table to 100
:
1 | UPDATE categories |
MySQL issued this error:
1 | Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`fkdemo`.`products`, CONSTRAINT `fk_category` FOREIGN KEY (`categoryId`) REFERENCES `categories` (`categoryId`) ON DELETE RESTRICT ON UPDATE RESTRICT)Code language: JavaScript (javascript) |
Because of the RESTRICT
option, you cannot delete or update categoryId 1
since it is referenced by the productId
1
in the products
table.
CASCADE
action
These steps illustrate how ON UPDATE CASCADE
and ON DELETE CASCADE
actions work.
\1) Drop the products
table:
1 | DROP TABLE products;Code language: SQL (Structured Query Language) (sql) |
\2) Create the products
table with the ON UPDATE CASCADE
and ON DELETE CASCADE
options for the foreign key:
1 | CREATE TABLE products( |
\3) Insert four rows into the products
table:
1 | INSERT INTO products(productName, categoryId) |
\4) Select data from the products
table:
1 | SELECT * FROM products;Code language: SQL (Structured Query Language) (sql) |
\5) Update categoryId
1 to 100 in the categories
table:
1 | UPDATE categories |
\6) Verify the update:
1 | SELECT * FROM categories;Code language: SQL (Structured Query Language) (sql) |
\7) Get data from the products
table:
1 | SELECT * FROM products;Code language: SQL (Structured Query Language) (sql) |
As you can see, two rows with value 1
in the categoryId
column of the products
table were automatically updated to 100
because of the ON UPDATE CASCADE
action.
\8) Delete categoryId
2 from the categories
table:
1 | DELETE FROM categories |
\9) Verify the deletion:
1 | SELECT * FROM categories;Code language: SQL (Structured Query Language) (sql) |
\10) Check the products
table:
1 | SELECT * FROM products;Code language: SQL (Structured Query Language) (sql) |
All products with categoryId
2 from the products
table were automatically deleted because of the ON DELETE CASCADE
action.
SET NULL
action
These steps illustrate how the ON UPDATE SET NULL
and ON DELETE SET NULL
actions work.
\1) Drop both categories
and products
tables:
1 | DROP TABLE IF EXISTS categories; |
\2) Create the categories
and products
tables:
1 | CREATE TABLE categories( |
The foreign key in the products
table changed to ON UPDATE SET NULL
and ON DELETE SET NULL
options.
\3) Insert rows into the categories
table:
1 | INSERT INTO categories(categoryName) |
\4) Insert rows into the products
table:
1 | INSERT INTO products(productName, categoryId) |
\5) Update categoryId
from 1 to 100 in the categories
table:
1 | UPDATE categories |
\6) Verify the update:
1 | SELECT * FROM categories;Code language: SQL (Structured Query Language) (sql) |
\7) Select data from the products
table:
The rows with the categoryId
1 in the products
table were automatically set to NULL
due to the ON UPDATE SET NULL
action.
\8) Delete the categoryId
2 from the categories
table:
1 | DELETE FROM categories |
\9) Check the products
table:
1 | SELECT * FROM products;Code language: SQL (Structured Query Language) (sql) |
The values in the categoryId
column of the rows with categoryId
2 in the products
table were automatically set to NULL
due to the ON DELETE SET NULL
action.
Drop MySQL foreign key constraints
To drop a foreign key constraint, you use the ALTER TABLE
statement:
1 | ALTER TABLE table_name |
In this syntax:
- First, specify the name of the table from which you want to drop the foreign key after the
ALTER TABLE
keywords. - Second, specify the constraint name after the
DROP FOREIGN KEY
keywords.
Notice that constraint_name
is the name of the foreign key constraint specified when you created or added the foreign key constraint to the table.
To obtain the generated constraint name of a table, you use the SHOW CREATE TABLE
statement:
1 | SHOW CREATE TABLE table_name;Code language: SQL (Structured Query Language) (sql) |
For example, to see the foreign keys of the products
table, you use the following statement:
1 | SHOW CREATE TABLE products;Code language: SQL (Structured Query Language) (sql) |
The following is the output of the statement:
As you can see clearly from the output, the table products
table has one foreign key constraint: fk_category
And this statement drops the foreign key constraint of the products
table:
1 | ALTER TABLE products |
To ensure that the foreign key constraint has been dropped, you can view the structure of the products table:
1 | SHOW CREATE TABLE products;Code language: SQL (Structured Query Language) (sql) |
Disabling foreign key checks
Sometimes, it is very useful to disable foreign key checks e.g., when you import data from a CSV file into a table. If you don’t disable foreign key checks, you have to load data into a proper order i.e., you have to load data into parent tables first and then child tables, which can be tedious. However, if you disable the foreign key checks, you can load data into tables in any order.
To disable foreign key checks, you use the following statement:
1 | SET foreign_key_checks = 0;Code language: SQL (Structured Query Language) (sql) |
And you can enable it by using the following statement:
1 | SET foreign_key_checks = 1;Code language: SQL (Structured Query Language) (sql) |
In this tutorial, you have learned about the MySQL foreign key and how to create a foreign key constraint with various reference options.