Assume that a computer manufacturer stores the product information of the whole machine and accessories in its database. The table used to save the product information of the entire machine is called pc; the table used to save the accessory supply information is called parts. Mysql foreign key
Assume that a computer manufacturer stores the product information of the whole machine and accessories in its da
foreign key field exists in the students table| State_lookup | it is the state field.--------------- A foreign key must refer to the primary key or unique
Label:MySQL foreign key set up detailed(1) Use of foreign keys: The role of the foreign key, there are two main: One is to let the database itself through the foreign key to ens
entered is not "referential integrity" check.
For example, there are two of tables
A (b): A is the primary key, and B is a foreign key (from b.b)
B (b,c,d): B primary Key
If I remove the foreign key attribute of
Add a foreign key for the added Data Table: syntax: altertable table name addconstraintFK_IDforeignkey (your foreign key field name) REFERENCES external table name (corresponding table's primary key
constraints. However, some of them are not perfect. For example, in the following situation. -- Restrict the exported TABLE -- restrict the TABLE 'room _ staus' -- alter table 'room _ staus' add constraint 'fk _ rooms_roomtype 'foreign KEY ('Hotel _ id ', 'Room _ id') REFERENCES 'room _ type' ('Hotel _ id', 'room _ id') on delete cascade on update cascade; alter table child add constraint
students.INSERT into student (id,name,class_id) value (1, "STU1", 2), (2, "STU2", 2), (3, "Stu3", 1), (4, "Stu4", 3), (5, "Stu5", 1), (6, " Stu6 ", 4);The rule now is that a class can correspond to multiple students, but a student can only belong to one class.5. Add foreign KEY constraints.Next, add a foreign key cons
Tags: foreign log existence ONS classification integrity use cannot category first, foreign key1foreign key: A field that links two tables, describing the primary foreign key relationship through the primary
add an index to the Parts table, the index is built on the model field, giving the cable a name called Idx_model.
Also similar to PC tables:
ALTER TABLE pc ADD INDEX Idx_cpumodel (Cpumodel);
In fact, these two indexes can be set when the table is created. Here just to highlight its necessity.
Defining foreign keys
The following is a set of "constraints" that are described above between two tables. Sin
First, let's take a look at what is the primary foreign key?1. Primary key: Uniquely identifies a row in a data table1) There can be only one primary key in a table. If you establish a primary key on another field, the original pr
MySQL discrete notes-foreign key Mysql foreign key
References: about MySQL foreign keys and mysql foreign key association creation
MySQL has the same definition of "
compressed Choose:Because MyISAM is relatively simple, it is better than innodb in efficiency. If the system reads more, write less. Low requirements for atomicity. So MyISAM the best choice. and the MyISAM recovery speed is fast. Can be restored directly with backup coverage.If the system reads less and writes more, especially when the concurrent writes are high. InnoDB is the first choice.Both types have their own pros and cons, choose the one that completely depends on your actual class. t
User_cons_columns. A table may or may not have constraints, and for each constraint on a table there is a record in user_constraints that describes the constraint. Includes the name of the table to which the constraint applies. If you know the constraint name and want to know the constraint type, query the User_constraints table. This view describes the definition of constraints. It does not provide constraints on which field names are defined. The c
A student's personal information table for a class:What is a foreign keyin the design, you add a foreign key to table 1, the foreign key is the Number field in table 2, then table 1 is the main table, table 2 is the child table. W
Label:from the database is a backup of the primary database, when the primary database changes from the database to be updated, these database software can design the update cycle. This is a means of improving information security. The master-slave database server is not geographically located, and the database can be saved when an accident occurs. relationship structure of the primary foreign key:1, one-to
Label: If Table A's primary key is a field in table B, the field is called the Foreign key of Table B, table A is called the primary table, and table B is called from the table. Foreign keys are used to achieve referential integri
code for adding a foreign key constraint to the room type (roomtype) column when you create a room information table (roominfo) is as follows:
"Create Table typeinfo
(
Roomid int primary key,
Roomtypeid int,
Roomprice numeric (7,2 ),
Roomstate varchar (2 ),
Pemark varchar (200 ),
Constraint pk_roominfo foreign
Tags: one explanation process cat localhost build includes password refPrevious paragraph reviewCREATE TABLE Table name (Field name 1 type [(width) constraint],Field Name 2 Type [(width) constraint],Field Name 3 Type [(width) constraint]);#解释:Type: What data type must be used to pass the value of the restriction fieldConstraint: A constraint is an additional rest
MySql multi-to-multi-link foreign key application Mysql foreign key
BitsCN.com
Business Requirements: The user table r_user stores user name and other information. Now you need to set up a work base for each user. one user can have multiple work bases, and multiple users can also have one work base, that is, many-to-ma
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.