当前位置:网站首页>MySQL constraint_ Foreign key constraint
MySQL constraint_ Foreign key constraint
2022-07-24 04:43:00 【Camellia——】
-- Foreign key constraints (foreign key)
-- Create an employee table employee
-- Employee number id, Name of employee name, Department name dept_name, Address of the employee address
CREATE TABLE employee(
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(10),
address VARCHAR(20),
dept_name VARCHAR(20)
);
-- Insert data into the employee table
INSERT INTO employee (NAME,address,dept_name)
VALUES(' article ',' Beijing ',' R & D department '),
(' Ke faxing ',' Xi'an ',' Testing department '),
(' Round and round ',' Shanghai ',' R & D department '),
(' Zhang San ',' Shenzhen ',' Implementation Department '),
(' Li Si ',' Guangzhou ',' Implementation Department '),
(' Zhao Youting ',' Hong Kong ',' Testing department ');
-- Query all data in the employee table
SELECT * FROM employee ;
DROP TABLE employee;-- Inquire about Employee's name and department name
SELECT
NAME ' full name ',
dept_name ' Department name '
FROM
employee ;
/*
problem :
id = 1 R & D department
id = 3 R & D department
id = 2 Testing department
id = 6 Testing department
...
Field redundancy ( Too much repetition ), The queried data is not very intuitive
*/
-- Improve the above problem : Split the employee table into two tables
-- The employee table : Record employee information
-- Departmental table : Record the number and name of the Department
DROP TABLE employee;-- Departmental table :dept surface : The primary key grows by itself id( Department number ) dept_name Department name
CREATE TABLE dept (
id INT PRIMARY KEY AUTO_INCREMENT,
dept_name VARCHAR (10)
) ;
DROP TABLE dept;
-- insert data
INSERT INTO dept (dept_name)
VALUES
(' R & D department '),
(' Testing department '),
(' Implementation Department ') ;
SELECT * FROM dept;-- Create an employee table
CREATE TABLE employee(
id INT PRIMARY KEY AUTO_INCREMENT, -- Employee number
NAME VARBINARY(10), -- Employee name
address VARBINARY(20), -- Employee address
dept_id INT -- Department number
);
INSERT INTO employee (NAME,address,dept_id)
VALUES(' article ',' Beijing ',1),
(' Ke faxing ',' Xi'an ',2),
(' Round and round ',' Shanghai ',1),
(' Zhang San ',' Shenzhen ',3),
(' Li Si ',' Guangzhou ',3),
(' Zhao Youting ',' Hong Kong ',2);SELECT *FROM employee;
SELECT
NAME ' full name ',
dept_id ' Department number '
FROM
employee ;-- Continue to improve : To find a way, you need to set the department number primary key in the Department table id And department table of employee table dept_id The relationship between
-- Solution : Add foreign key
-- Main table ( Departmental table :dept), From the table employee( The table where the foreign key is located )
-- constraint Statement Name of the foreign key ( Main table name _ From table name _fk)
-- foreign key ( From the fields of the table ) reference The name of the main table ( Primary key name );
DROP TABLE employee;
-- Create an employee table and add foreign key constraints
CREATE TABLE employee(
id INT PRIMARY KEY AUTO_INCREMENT,-- Employee number
NAME VARBINARY(10), -- Employee name
address VARCHAR(20),-- Employee address
dept_id INT,
CONSTRAINT -- Statement
dept_emp_fk FOREIGN KEY(dept_id)
REFERENCES dept(id)
);-- Add data to the employee table
INSERT INTO employee (NAME,address,dept_id)
VALUES(' article ',' Beijing ','1'),
(' Ke faxing ',' Xi'an ','2'),
(' Round and round ',' Shanghai ','1'),
(' Zhang San ',' Shenzhen ','3'),
(' Li Si ',' Guangzhou ','3'),
(' Zhao Youting ',' Hong Kong ','2');SELECT *FROM dept;
SELECT*FROM employee;
-- Add data ( Department number must exist , Otherwise, the report will be wrong !)
INSERT INTO employee (NAME,address,dept_id) VALUES (' Ma Yili ',' Xi'an ','4');
-- Due to saving foreign keys , You cannot directly add or modify slave table data ( Related to fields declared by foreign keys )
-- First, operate the main table ----> Then you can operate the slave table
-- Add operation
-- Main table
INSERT INTO dept(dept_name)VALUES(' The sales department ');-- Delete foreign key
-- alter table From table name DROP FOREIGN KEY Name of the foreign key
ALTER TABLE employee DROP FOREIGN KEY dept_emp_fk;-- adopt sql Statement to add foreign keys
ALTER TABLE employee
ADD CONSTRAINT -- Statement
dept_emp_fk FOREIGN KEY(dept_id)
REFERENCES dept(id);
边栏推荐
- OWA dynamic password SMS authentication scheme solves the problem of outlook email two factor authentication
- In the business interaction and foreign service of.Gz, we integrate multiple models
- What if the computer time is often inaccurate? Set up tutorials to automatically update and proofread computer time
- [dish of learning notes, dog learning C] Dachang written test, is that it?
- Common cross domain problems
- C language classic exercises to write a program to find all the perfects within 1000.
- [JDBC] error exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.communicationsexception: communica
- .gz的业务交互和对外服篇中我们通合多个模型
- 可以脱离设据生效这些都是控化部署能力,而后引如
- C语言:冒泡排序法
猜你喜欢

Basic syntax of MySQL DDL and DML and DQL

基于GL Pipeline与光线追踪技术的融合实现的台球模拟器

Design of high frequency small signal resonant amplifier course design Multisim Simulation

Basic learning notes of C language

《论文复现》BiDAF代码实现过程(3)模型建立

64 attention mechanism 10 chapters

归并排序(Merge sort)

Will your NFT disappear? Dfinity provides the best solution for NFT storage

C语言经典习题之猴子吃桃问题

Nautilus 3.19.2 adds momentum to Gnome
随机推荐
64 attention mechanism 10 chapters
PHP修改配置文件的两种方法
Post SQL era: edgedb 2.0 Release Notice
What is the real HTAP? (2) Challenge article
An online accident, I suddenly realized the essence of asynchrony
基于GL Pipeline与光线追踪技术的融合实现的台球模拟器
Determined by hardware (see official 2 and
Esp32:arduino tutorial summary
激活函数和最常用的10个激活函数
Godson leader spits bitterness: we have the world's first performance CPU, but unfortunately no one uses it!
Why can't I log on my baidu account? Introduction to the solution of baidu account unable to log in
Pony activation tool appears cannot open file k:\oemsf solution
Journey of little black leetcode: 341. Flattening nested list iterator
Nautilus 3.19.2 adds momentum to Gnome
greatest common divisor
几种常见的排序
How to make the words on the screen larger (setting method to make the text more comfortable on the large screen)
How long has it been since you changed your cell phone?
Good performance and reliability. The parameter that initiates writing IIC co type code and is -4
Personalized customized TenPay name customized TenPay is a diagram of any name such as Ma Huateng