Get the App
SLTechnology News&Howtos  ›  Database  › 

How to establish Foreign key constraints in mysql

Shulou Source: shulou.com Published: 2022-06-01 02:39:48 10月21日 Update

Editor to share with you mysql how to establish foreign key constraints, I hope you will learn a lot after reading this article, let's discuss it together!

Mysql's method of establishing foreign key constraints: directly execute [CREATE TABLE stu (sid INT PRIMARY KEY,NAME VARCHAR (50) NOT NULL);] statement.

Add a foreign key constraint

CREATE TABLE stu (sid INT PRIMARY KEY, NAME VARCHAR (50) NOT NULL)

Add foreign key constraint mode 1

CREATE TABLE score1 (score DOUBLE, sid INT, CONSTRAINT fk_stu_score1_sid FOREIGN KEY (sid) REFERENCES stu (sid))

Add foreign key constraint mode 2 (this can be used if the table already exists)

CREATE TABLE score1 (score DOUBLE, sid INT); ALTER TABLE score1 ADD CONSTRAINT fk_sid FOREIGN KEY (sid) REFERENCES stu (sid) has finished reading this article. I believe you have some understanding of how to establish foreign key constraints in mysql. You want to know more about it. Welcome to follow the industry information channel. Thank you for reading!

Tags: Methods articles finished methods more knowledge industry sentences information information channels channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology vpn Microsoft Apple Huawei