当前位置:网站首页>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 .
边栏推荐
- Introduction to smart contract security audit delegatecall (2)
- Text editor for QT project practice -- Episode 9
- 启动服务11111
- Text editor of QT project practice ---------- episode 8
- Bi-sql - different join
- 15. several methods of thread synchronization
- Text editor for QT project practice - Episode 10
- 108页(4万字)未来公寓智能化设计平台项目方案建议书2022版
- Transform BeanUtils to achieve list data copy gracefully
- Yasea APK Download Image
猜你喜欢
Bi-sql between
Danish Technical University pioneered the application of quantum computing to power flow modeling of energy system
Bi-sql - different join
Rich text tables, lists, pictures
Bi-sql Union
粉丝福利,JVM 手册(包含 PDF)
15.线程同步的几种方法
51单片机多机通信
2022 simulated 100 questions of safety officer-c certificate examination and online simulated examination
2022R1快开门式压力容器操作考题及答案
随机推荐
Scala sample class
2022 melting welding and thermal cutting recurrent training question bank simulated examination platform operation
Scala object blending trait
用手机在同花顺上开户靠谱吗?这样炒股有没有什么安全隐患
Examination questions and mock examination for safety management personnel of hazardous chemical business units in 2022
ContentResolver,拿到手机短信内容
Text editor for QT project practice -- Episode 9
4 ans d'expérience de travail, 5 modes de communication Multi - thread ne peuvent pas être décrits, vous osez croire?
最新QQ微信域名防红PHP程序源码+强制跳转打开
Tencent has completed the comprehensive cloud launch to build the largest cloud native practice in China
ImageView shows network pictures
腾讯云WeCity丨你好 2022!
Bi-sql select into
AUTOCAD——两种延伸方式
matlab 取整
启动服务11111
If the order has not been paid for 30 minutes, it will be automatically cancelled. How can I achieve this?
Syntax highlighting of rich text
Scala sample object
戴尔为何一直拒绝将商用本的超薄推向极致?