当前位置:网站首页>Bi SQL constraints

Bi SQL constraints

2022-06-25 01:15:00 Powerbi white tea

Constraints

Constraints, Binding meaning . It is usually used to constrain the data added to the table , If it conflicts with the constraint , The behavior will be terminated .

Usually ,Constraints Will be with CREATE TABLE or ALTER TABLE Use it with .

Constraints It has always been a very important concept in data warehouse , Is used to manage how to insert or process database data .

Need to know , For databases , The most important thing is the normalization and standardization of database data , This will determine the scalability and reusability of the current data .

CREATE TABLE: Create table statement .

ALTER TABLE: Update table statement .

Constraints The following constraints are often used in data warehouses :

  • NOT NULL( Not empty )
  • UNIQUE( only )
  • PRIMARY KEY( Primary key )
  • FOREIGN KEY( Foreign keys )
  • CHECK( Check )
  • DEFAULT( The default value is )

Next, let's take a look at the usage scenarios of different constraints .

A.NOT NULL

NOT NULL Will constrain the column not to accept null values , So when inserting data or updating data , If the column constraint is NOT NULL, Then the insert and update behavior will be terminated .

grammar

CREATE TABLE  The name of the table 
(
 Column name   data type  NOT NULL,
 Column name   data type  NOT NULL,
......
)

Using examples

CREATE TABLE WHITETEST (TEST INT NOT NULL, ID INT)

With the above statement, we created a file named WHITETEST Table of , And to TEST Add constraint is not empty .

We're going to talk about WHITETEST Table to update data .

INSERT INTO WHITETEST (TEST , ID) VALUES ( NULL , 13)

In the data warehouse, we execute the above statement , The following results will be returned :

Because the column limits TEST Not empty , Therefore, the data cannot be updated .

Let's insert other data to see .

INSERT INTO WHITETEST (TEST , ID) VALUES ( 16 , 13)

give the result as follows :

We can see clearly that , Data insertion successful .

B.UNIQUE And PRIMARY KEY

UNIQUE The data of the column will be constrained to exist as a unique identifier .

PRIMARY KEY Exists as the primary key of the table .

Be careful

  • UNIQUE And PRIMARY KEY The difference is big .
  • PRIMARY KEY Will bring it UNIQUE Properties of .
  • There can be more than one column in the table UNIQUE Constraints , But there can only be one PRIMARY KEY.

In other words, it is the primary key of the dimension ( Unique identification ) There can be multiple columns , But as the primary key of a table, there can only be one .

  • PRIMARY KEY The primary key values of any two rows cannot be the same
  • Each line needs to have one PRIMARY KEY
  • PRIMARY KEY Columns of are never modified and updated
  • PRIMARY KEY Reuse is not allowed , When the primary key value is deleted, it cannot be used for new rows
  • UNIQUE Can contain NULL value
  • UNIQUE Modifiable update
  • UNIQUE reusable

for example :

We have an order form , There are orders ID, So now the order ID have access to UNIQUE Constraint , To guarantee the order ID No repetition . But we need an additional primary key to mark each row of records in the current table . So that other tables can reference the data in this table .

notes : Order ID It can also be used as a primary key , But the information may be too long , And there may be situations where information is used .

grammar
UNIQUE grammar

CREATE TABLE  The name of the table 
(
 Column name   data type  NOT NULL UNIQUE,
 Column name   data type  NOT NULL,
CONSTRAINT uc_ Constraint name  UNIQUE ( Column name , Column name ...)// Multiple columns define constraints 
......
)

PRIMARY KEY grammar

CREATE TABLE  The name of the table 
(
 Column name   data type  NOT NULL PRIMARY KEY,
 Column name   data type  NOT NULL,
CONSTRAINT pk_ Constraint name  PRIMARY KEY ( Column name , Column name ...)// Multiple columns define constraints 
......
)

C.FOREIGN KEY

Foreign keys in a table , It can usually be used to connect the primary keys in another table .

for instance :

We have one Fact A watch and a Product surface , Two tables go through ProductID Association .

that Product In the table ProductID It's the primary key ,Fact In the table ProductID It's the foreign key .

meanwhile , The existence of foreign keys , It can prevent accidental deletion . This is for the data warehouse , Very important .

grammar

CREATE TABLE  The name of the table 
(
 Column name   data type  FOREIGN KEY REFERENCES  The name of the table ( Primary key ID),
 Column name   data type  NOT NULL,
CONSTRAINT fk_ Constraint name  FOREIGN KEY ( Foreign keys )
REFERENCES  The name of the table ( Primary key )// Multiple columns define constraints 
......
)

D.CHECK

CHECK It is usually used to restrict the range of column values .

grammar

CREATE TABLE  The name of the table 
(
 Column name   data type  NOT NULL CHECK ( Column name >0),
 Column name   data type  NOT NULL,
CONSTRAINT chk_ Constraint name  CHECK ( Column name >0, Column name =' Conditions ')// Multiple columns define constraints 
......
)

E.DEFAULT

DEFAULT It can usually be used to constrain the default values of columns , If no other values are inserted , The default value will appear in all records .

grammar

CREATE TABLE  The name of the table 
(
 Column name   data type  NOT NULL DEFAULT ' The default value is ',
 Column name   data type  NOT NULL,
......
)

The usage of constraints is almost the same here , Because of the time limit, I didn't enumerate various use examples for my friends , But I suggest you to try .

This is white tea , One PowerBI Beginners .

原网站

版权声明
本文为[Powerbi white tea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210542593851.html