SQL Beginner Tutorial Learning (iv)

Source: Internet
Author: User

1. Union,union All

SELECT E_name from Employees_china
UNION
SELECT E_name from Employees_usa

By default, the UNION operator chooses a different value. If duplicate values are allowed, use UNION all.

2. Create a table

CREATE TABLE Persons
(
id_p int not NULL,
LastName varchar (255) is not NULL,
FirstName varchar (255),
Address varchar (255),
City varchar (255),
CONSTRAINT uc_PersonID UNIQUE (Id_P,LastName)
)

If you need to name a unique constraint and define a UNIQUE constraint for multiple columns

3. Create a UNIQUE constraint in the "id_p" column when the table has been created

ALTER TABLE Persons
ADD CONSTRAINT uc_PersonID UNIQUE (Id_P,LastName)

If you want to revoke a UNIQUE constraint

ALTER TABLE Persons

DROP CONSTRAINT uc_PersonID

4.CREATE TABLE Persons
(
id_p int not NULL PRIMARY KEY ,
LastName varchar (255) is not NULL,
FirstName varchar (255),
Address varchar (255),
City varchar (255)
)

5.SQL FOREIGN KEY Constraint

CREATE TABLE Orders
(
id_o int not NULL PRIMARY KEY,
OrderNo int not NULL,
Id_P int FOREIGN KEY REFERENCES Persons(Id_P)
)

, the id_p column in Orders points to the id_p column in the Persons table.

The "id_p" column in the "Persons" table is the PRIMARY KEY in the Persons table.

The "id_p" column in the Orders table is the FOREIGN KEY in the Orders table.

The FOREIGN KEY constraint is used to prevent actions that disrupt the connection between tables.

Http://www.w3s.com.cn/sql/sql_foreignkey.asp

SQL Beginner Tutorial Learning (iv)

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.