The primary and Foreign keys in the database exist to ensure the integrity of the database. When a table has a foreign key, it means that this table is restricted. When we operate on the database data, we should note that when we first learn the database, we always want to use more than one key, foreign keys, and other constraints, can be added, and many errors were found during system creation. Image Cascade
The primary and Foreign keys in the database exist to ensure the integrity of the database. When a table has a foreign key, it means that this table is restricted. When we operate on the database data, we should note that when we first learn the database, we always want to use more than one key, foreign keys, and other constraints, can be added, and many errors were found during system creation. Image Cascade
The primary and Foreign keys in the database exist to ensure the integrity of the database. When a table has a foreign key, it means that this table is restricted. When we operate on the database data, we should note that when we first learn the database, we always want to use more than one key, foreign keys, and other constraints, can be added, and many errors were found during system creation. Such as cascading deletion, adding data restricted by foreign keys, etc. Therefore, the primary and Foreign keys in the database must be used with caution.
SQL server has been used since the system was started. However, we only use the functions of one road edge. During the development of the cooperative edition, the database was officially valued for the first time. Strictly follow the software engineering process, from the conceptual model to the physical model, to the establishment of database tables. It also uses some professional database design software, such as powerdesigner. After the conceptual model is designed, it needs to be converted into a physical model.
Concept Model to Physical Model
The data room charging system has several tables: Consumption table, back-to-card table, recharge table, and Bill table. At the time of checkout, we collect statistics on the Consumption table, recharge table, and unchecked information in the return table to obtain the data and insert it into the Bill table. After the checkout, mark the data in the three tables as checked out. The relationship between them is many-to-one, and there is a foreign key constraint.
Convert to physical model:
The conceptual model is converted to a physical model. The foreign key constraints in the conceptual model are displayed in the consumption table, recharge table, and debit label in the return table. The checkout number is the primary key in the Bill table. In this way, the consumption table, refund table, recharge table, and Bill table ensure the integrity between databases.
The problem is: the data in the foreign key table is first inserted, and the data in the Bill table is then inserted. This means that when we insert a foreign key table record, the "checkout number" does not exist, but the foreign key constraint. When we insert data, the foreign key must be an existing primary key.
Can we set the foreign key to "NULL" when inserting a record?
A foreign key constraint is directly set up in Sqlserver. If the foreign key of the inserted record is "NULL", it is not allowed to create a foreign key from the database graph and then set the "NULL" value, this is all done.
Specific operations for setting NULL values of Foreign keys
1. Create a database relationship diagram:
2. Right-click the selected table and choose table View> standard. The relationship diagram is as follows:
3. Set the NULL value in the red area in the figure: In, the foreign key "news title" of the News Comment table is the primary key in the News table. Here it can be set to "NULL". The foreign key can be inserted as NULL during database insertion.
Exercise caution when using database Constraints
All discussions share the same feelings. Therefore, when using database constraints, such as primary and Foreign keys, we should analyze them based on specific conditions. The system is not very large and the data requirements are not very strict, we can have fewer constraints to facilitate system operation and operation. If the system has strict requirements on the database, we must strictly follow the three paradigm and add the necessary primary and Foreign keys, ensure data integrity.