Introduction to two ways of creating Foreign Keys by constraining Mysql
Next, let's learn about the two ways of constraining Mysql to create foreign keys. I believe you will benefit a lot after reading it. The text is not much in essence. I hope you can restrict Mysql to create foreign keys in two ways. This short article is what you want.
By adding foreign key constraints to the table field of mysql, the consistency and integrity of the data can be effectively maintained, and the data will not be prone to problems.
1. Create a foreign key constraint directly when creating a table
Create table books (bookid number (10) not null primary key, bookName varchar2 (20) not null, price number (10dint 2), categoryId number (10) not null references Category (id)-Foreign key constraint)
Note: you must create a reference table before you can create a foreign key constraint, that is, you must have an existing table Category, followed by book
2. Create the table first. After the table is created successfully, add the foreign key constraint separately.
Create table books (bookid number (10) not null primary key, bookName varchar2 (20) not null, price number (10dag2), categoryId number (10) not null); ALTER TABLE books ADD CONSTRAINT FK_Book_categoryid FOREIGN KEY (categoryId) REFERENCES Category (id)
The above two ways are currently the way to add foreign key constraints in Mysql. I hope that when you use the associated table in the future, you can add foreign key constraints to some fields of the table, so that the data can maintain integrity.
After reading this article on the two ways of constraining Mysql to create foreign keys, many readers will want to know more about it. For more industry information, you can follow our industry information section.