What does it mean not to use foreign keys in Internet development?

it is said that Internet developers try to not to use foreign keys , so what does the here mean by not using foreign keys ?
1. The meaning here is clear:
No foreign key constraints . For example, when deleting data in one table, if you want to cascade delete the associated data in another table, in the past, it was cascaded by the database, and now it should be moved to the program to maintain the consistency of the data.

2. Another meaning:
the definition of a foreign key is that a field in one table is the primary key in another table. If you only follow the literal understanding of the words " do not use foreign keys ", you should extract the foreign key fields and put them in a intermediate table . to put it simply, they are all treated as many-to-many.
such as

(userid,username)  
(orderid,totalPrice,userid)

Question1: here useid is a foreign key. Should it be designed like this if you don"t use a foreign key?

(userid,username)  
(orderid,totalPrice)
-(userid,orderid)

Question2: if so, if a table has multiple foreign keys, won"t there be many intermediate tables?

all over Baidu, it is said not to use foreign keys, not to use foreign keys, so it makes me very confused. I hope you will greatly solve the problem. Thank you!


it is said that Internet developers try not to use foreign keys, so what does it mean to use no foreign keys here?

the foreign key here refers to the foreign key constraint of the database.

No foreign key constraints are required. For example, when deleting data in one table, if you want to cascade delete the associated data in another table, in the past, it was cascaded by the database, and now it should be moved to the program to maintain the consistency of the data.

Yes. The constraint relationship of foreign keys is no longer maintained by the database, but by the application.

the definition of a foreign key is that a field in one table is the primary key in another table. If you only follow the literal understanding of the words "do not use foreign keys", you should extract the foreign key fields and put them in an intermediate table. To put it simply, they are all treated as many-to-many.

No. How to create the table is the same as before, but do not establish foreign key constraints where you need to establish foreign key constraints.

for example, our original statement goes like this:

CREATE TABLE `user` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '',
  `user_name` varchar(50) NOT NULL DEFAULT '' COMMENT '',
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `order` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '',
  `total_price` decimal(10,2) NOT NULL DEFAULT '0.00',
  `user_id` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `for_indx_user_id` (`user_id`),
  CONSTRAINT `for_indx_user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

is not constrained by a foreign key:

CREATE TABLE `user` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '',
  `user_name` varchar(50) NOT NULL DEFAULT '' COMMENT '',
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `order` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '',
  `total_price` decimal(10,2) NOT NULL DEFAULT '0.00',
  `user_id` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
After

does not apply foreign key constraints, we usually add an index to fields that do not establish foreign key constraints in order to speed up the query.

CREATE TABLE `order` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '',
  `total_price` decimal(10,2) NOT NULL DEFAULT '0.00',
  `user_id` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `idx_user_id` (`user_id`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

if you understand, your following problems will naturally disappear.

avoid using foreign keys, and you can maintain constraints programmatically when inserting data.

advantages of using foreign key constraints:

  • Foreign keys can save development
  • Foreign keys can restrict the validity of data, and illegal data cannot be inserted

disadvantages of using foreign keys to constrain:

  • has extra overhead
  • when the primary key table is locked, the foreign key table is also locked
  • when deleting the data of the primary key table, you need to delete the data of the foreign key table first
  • when you modify a foreign key table field, you need to rebuild the foreign key constraint

in actual development, foreign key constraints are generally not established.


I took a look. Most of the words "foreign keys" on the Internet refer to "foreign key constraints". Maintaining associations in code should not be called foreign keys, which should be limited to databases.

this post makes it clear that performance issues, deadlock issues, and so on, are only problems with "foreign key constraints".
https://www.zhihu.com/questio.


means that you don't need the foreign key of the database. When you build a table, you don't need CONSTRAINT. You can write what you want to write after that. Whether you want to build an intermediate table or not depends on whether the specific business needs


.

unlike some material systems and personnel systems in traditional industries, Internet applications are characterized by a large amount of data, which may require insert millions of pieces of data every day. If you use foreign keys, you have to scan for foreign key conflicts every time you insert the database, and the efficiency is very slow ~

.

so generally speaking, it is a common practice to package the operations of these tables into a system without foreign keys, manage the data in one place, and ensure the correctness of the data at the code layer

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3a020-40767.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3a020-40767.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?