Simple tutorial on SQL statements in linux --- primary key, foreign key, and SQL
Primary Key)Each item in is a unique value in the table. In other words, it is used to uniquely identify each row of data in a table. A primary key can be a column in the original data, or a column created by a person (a column irrelevant to the original data ). A primary key can contain one or more columns. When a primary Key contains multiple columns, it is called a Composite Key ).
The primary key can be set when a new table is created (UseCreate table statement), Or to change the existing table architecture (usingALTER TABLE).
The following describes how to set the primary key when creating a new table:
MySQL:
Create table Customer
(SID integer,
Last_Name varchar (30 ),
First_Name varchar (30 ),
Primary key (SID ));
Oracle:
Create table Customer
(SID integer primary key,
Last_Name varchar (30 ),
First_Name varchar (30 ));
SQL Server:
Create table Customer
(SID integer primary key,
Last_Name varchar (30 ),
First_Name varchar (30 ));
The following describes how to change the existing table schema to set the primary key:
MySQL:
Alter table Customer add primary key (SID );
Oracle:
Alter table Customer add primary key (SID );
SQL Server:
Alter table Customer add primary key (SID );
Note thatALTER TABLEBefore adding a primary key, we need to confirm that the column used as the primary key is set to "not null"; that is to say, that column must NOT have no data.
A foreign key is a field (or multiple) that points to the primary key of another table. The foreign key is used to determine the integrity of the reference (referential integrity ). In other words, only permitted data values are stored in the database.
For example, suppose we have two tables: oneCUSTOMERThe table records the information of all customers.ORDERSThe table records the information ordered by all customers. One restriction here is that all the customers in the ordering materials must followCUSTOMERThe table exists. Here, we willORDERSSet a foreign key in the table, which is directedCUSTOMERThe primary key in the table. In this way, we can determine allORDERSAll customers in the table exist.CUSTOMERTable. In other words,ORDERSNo customer in the table does not existCUSTOMERInformation in the table.
The two tables are structured as follows:
CUSTOMER table
Column name |
Nature |
SID |
Primary Key |
Last_Name |
|
First_Name |
|
ORDERS table
Column name |
Nature |
Order_ID |
Primary Key |
Order_Date |
|
Customer_SID |
Foreign key |
Amount |
|
In the above example,ORDERSThe Customer_SID field in the table is a pointCUSTOMERForeign key of the SID field in the table.
The following listORDERSTable-specific foreign keys:
MySQL:
CREATE TABLE ORDERS
(Order_ID integer,
Order_Date date,
Customer_SID integer,
Amount double,
Primary key (Order_ID ),
Foreign key (Customer_SID) references customer (SID ));
Oracle:
CREATE TABLE ORDERS
(Order_ID integer primary key,
Order_Date date,
Customer_SID integer references customer (SID ),
Amount double );
SQL Server:
CREATE TABLE ORDERS
(Order_ID integer primary key,
Order_Date datetime,
Customer_SID integer references customer (SID ),
Amount double );
In the following example, the foreign key is specified by changing the table schema. Assume thatORDERSThe table has been created, but the foreign key has not been specified:
MySQL:
ALTER TABLE ORDERS
Add foreign key (Customer_SID) references customer (SID );
Oracle:
ALTER TABLE ORDERS
ADD (CONSTRAINT fk_orders1) foreign key (Customer_SID) references customer (SID );
SQL Server:
ALTER TABLE ORDERS
Add foreign key (Customer_SID) references customer (SID );
SQL DROP TABLE>
Reprinted, please note: Xiao Liu
Bytes