Data application master's SQL basic tutorial shares 4-key constraints

key constraints

5. Primary key constraints

【Introduction to knowledge points】

We mentioned primary keys in the database chapter 1, and SQL is no exception.
A primary key in SQL is one or more columns (fields) in a table that are used to implement unique markers. The primary key is specified when the table is created.
Taking the Student table we created as an example, we set the ID as the primary key:

CREATE TABLE Student
(
ID INT NOT NULL PRIMARY KEY,
SName VARCHAR(25) NOT NULL,
Sex VARCHAR(10),
Major VARCHAR(20),
Credit INT
);

-- or

CREATE TABLE Student
(
ID INT NOT NULL,
SName VARCHAR(25) NOT NULL,
Sex VARCHAR(10),
Major VARCHAR(20),
Credit INT,
PRIMARY KEY (ID)
);

 


The reason why the ID is used as the primary key means that the ID can be used as a unique identifier, because everyone's ID is a different value. In this way, the accuracy and consistency of the data in the Student table can be ensured, and the data of each row can be fully expressed (displayed) through the ID.

6. Uniqueness constraint

【Introduction to knowledge points】

UNIQUE, the unique constraint, requires that the value of a column in the table is unique in each row, which is similar to the primary key constraint.
The usage of UNIQUE is similar to the primary key. Take the Student table as an example:

CREATE TABLE Student
(
ID INT NOT NULL UNIQUE,
SName VARCHAR(25) NOT NULL,
Sex VARCHAR(10),
Major VARCHAR(20),
Credit INT
);

-- or

CREATE TABLE Student
(
ID INT NOT NULL,
SName VARCHAR(25) NOT NULL,
Sex VARCHAR(10),
Major VARCHAR(20),
Credit INT,
UNIQUE (ID)
);

 

The example is only to illustrate the usage of UNIQUE, it has no practical significance and is for reference only.

Of course, there are many differences between the primary key constraint and the unique constraint:
The column of the unique constraint allows the value of NULL, but the primary key does not;
The unique constraint can set multiple columns, but they will not be used as the primary key
; In general, a table can have multiple unique constraints, but only one primary key (of course, there can be multiple columns or fields to form a composite primary key);
The primary key also has a key role, which is to be referenced by foreign keys .

So, let's learn about foreign keys.

7. Foreign key constraints

【Introduction to knowledge points】

What is a foreign key? The foreign key actually exists based on the primary key. The primary key that exists in table 1 also exists in table 2, so it becomes a foreign key of table 2.

In other words, the foreign key FOREIGN KEY in one table points to the primary key PRIMARY KEY in another table, which actually means that a subclass inherits the parent class.

Let's take an example. Suppose we also have a table FinanceStu (financial student table), which has columns such as ID, SName, Class (class), etc. The ID of the table is its own primary key, and it is also a foreign key to Student:

CREATE TABLE FinanceStu
(
ID INT NOT NULL,
SName VARCHAR(25) NOT NULL,
Sex VARCHAR(10),
Class VARCHAR(10),
Credit INT,
PRIMARY KEY (ID),
FOREIGN KEY (ID) REFERENCES Student(ID)
);

 

So we can think of Student as a parent table, and FinanceStu is a child table based on Student. If we delete the ID in the parent table, the ID in the child table will also be deleted. This is the association and constraints mentioned in the update table and deletion table before. This is why we emphasize that when designing the table An important reason to be thoughtful.

8. CHECK constraints

【Introduction to knowledge points】

The CHECK constraint is used to specify the value range of a column. It can specify the value range of a column, or it can specify the value range of multiple columns:

CHECK (column specification)
-- CHECK constraint on a column

CONSTRAINT convention name CHECK (column 1 specified, column 2 specified, column 3 specified)
-- CHECK constraints on one or more columns

 

For example, we want to make a CHECK constraint on the ID in the Student table, requiring that its value cannot be lower than 20160000, and the Credit cannot be lower than 0 (but it is allowed to be empty), we can write:

CREATE TABLE Student
(
ID INT NOT NULL,
SName VARCHAR(25) NOT NULL,
Sex VARCHAR(10),
Major VARCHAR(20),
Credit INT,
CONSTRAINT mycheck CHECK (ID > 20160000,Credit > 0)
);

 

With such a constraint, we need to follow this constraint when adding and modifying row data.

9. Summary

【Introduction to knowledge points】

We introduce some SQL statements for manipulating tables in these sections, using CREATE TABLE to create new tables, ALTER TABLE to change table column objects, and DROP TABLE to drop a table completely.
But we have to note that we must use these statements carefully, and we should make backups when using them.
Since the syntax of these statements is different in different database management systems (DBMS), you may wish to query some corresponding DBMS documents for more detailed information.

In addition, about the constraints of keys (columns), constraints seem to be relatively simple, but they play a crucial role in the integrity and relevance of database tables.

 

To be continued below. . . . . .

 

Welcome to visit our official website:

http://www.datanew.com/datanew/homepage

http://www.lechuangzhe.com/homepage

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326343600&siteId=291194637