Oracle Master-Slave table primary foreign key correspondence relationship

Source: Internet
Author: User

First, let's take a look at what is the primary foreign key?

1. Primary key: Uniquely identifies a row in a data table

1) There can be only one primary key in a table. If you establish a primary key on another field, the original primary key is canceled.  In Access, although the primary key is not required, it is a good idea to set a primary key for each table. 2) The value of the primary key cannot be duplicated, nor is it null (NULL).  The primary key is optional and can be defined in the CREATE table or ALTER table statement. 3) Primary key creation: How to set the primary key when a new table is being built mysql:create table Customer (SID integer,last_name varchar (), first_name varchar (30),PRIMARY KEY (SID)); Oracle:create TABLE Customer (SID integer PRIMARY KEY, last_name varchar (+), first_name varchar (30)); SQL server:create TABLE Customer (SID integer PRIMARY KEY, last_name varchar (+), first_name varchar (30)); 2. Foreign key:if the public keyword is the primary key in a relationship, then this common keyword is called the foreign key of the other relationship. Thus, the foreign key represents the correlation between the two relationships. A table with a foreign key of another relationship as the primary key is called the primary table, and a table with a key in it is called the primary table from the table. Foreign keys are also referred to as foreign keywords .   Features: 2.1. Create a foreign key for the associated field.  2.2. All keys must be unique.  2.3. Avoid the use of composite keys.     2.4. The foreign key is always associated with a unique key field. CREATE TABLE TABLE1 ([id] int IDENTITY (PRIMARY KEY) CREATE TABLE TABLE2 ([id] int not NULL,FOREIGN KEY ([id]) REFERENCES [TABLE1] ([ID ]))TABLE1 is referenced in TABLE2, where TABLE1 is the primary key table, and TABLE2 is the foreign key table.         Summary: The primary key table is the referenced table, and the foreign key table is a table that references other tables. Two. With the base reserve above, let's take a look at the main foreign key correspondence between master and slave table before making a multi-table join query, we need to deal with multiple tables, for example, determine the master-slave relationship, establish a foreign key, join the determination of the intermediate table and other operations.

In general, build the table when the main table, and then built from the table (the same when inserting data), how to determine the master-slave relationship, further establish foreign key constraints?

For example, there are three tables, the Customers table (CUSTOMERID--PK)-The primary table for the Orders table and the OrderItems table, and the latter two for the table

Orders table (ORDERID--PK;CUSTOMERID--FK)--The primary table for the OrderItems table

OrderItems table (Orderitemid---pk;bookid,quantity,orderid---fk),

  Note: A table can have only one primary key, but there may be multiple foreign keys

From these three tables, the Orders table's CustomerID is listed as a foreign key, which comes from the primary key column of table Customers CustomerID, so this is what we often call the foreign key reference primary key, the referenced table is the primary table, and the referenced table is from the table. That is, the Customers table is the main table. The Orders table is from the table; in the same way, the OrderID in the OrderItems table is listed as a foreign key from the primary key column of the (referenced) table orders OrderID, so for both tables, the Orders table is the primary table, and the OrderItems table is from the table.

for the order of the deletion of the list, just as opposed to the order of the table, delete the main table from the table, that is, for the above three tables, first delete the table orderitems, then delete the table orders, and finally delete the Customers form.

In a query operation for a multi-table join, we can use the Orders table as the intermediate table because he is related to the other two tables. That is:

SELECT * FROM orders a join customers C

On A.customerid=c.customerid

Join OrderItem T

On T.orderid=a.orderid

The above is from the network finishing and personal summary, thank http://blog.csdn.net/luoxl81/article/details/6876930 author

  

Oracle Master-Slave table primary foreign key correspondence relationship

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.