当前位置:网站首页>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 .
边栏推荐
- bindservice方法实现音乐播放暂停
- 4 ans d'expérience de travail, 5 modes de communication Multi - thread ne peuvent pas être décrits, vous osez croire?
- Examination questions and mock examination for safety management personnel of hazardous chemical business units in 2022
- Tencent has completed the comprehensive cloud launch to build the largest cloud native practice in China
- Contentresolver, get the SMS content
- “一个优秀程序员可抵五个普通程序员!”
- matlab 取整
- 汇编语言(4)函数传参
- Powerbi - for you who are learning
- Introduction to bi-sql wildcards
猜你喜欢

Basic knowledge of assembly language (2) -debug

Linux64Bit下安装MySQL5.6-不能修改root密码

Zuckerberg demonstrated four VR head display prototypes, and meta revealed the "family" of metauniverse

JVM指令

If the order has not been paid for 30 minutes, it will be automatically cancelled. How can I achieve this?

汇编语言(4)函数传参

LLVM TargetPassConfig

I brush the question I - copy the linked list with random pointer

Danish Technical University pioneered the application of quantum computing to power flow modeling of energy system

重磅:国产IDE发布,由阿里研发,完全开源!(高性能+高定制性)
随机推荐
音频PCM数据计算声音分贝值,实现简单VAD功能
activity生命周期
2022年起重机司机(限桥式起重机)考试题库模拟考试平台操作
Scala trait inheritance class
汇编语言(4)函数传参
15. several methods of thread synchronization
使用 Loki 微服务模式部署生产集群
Scala classes inherit multiple attributes
Scala responsibility chain pattern
VB learning notes
最新QQ微信域名防红PHP程序源码+强制跳转打开
用手机在同花顺上开户靠谱吗?这样炒股有没有什么安全隐患
汇编语言(2)基础知识-debug
2022 melting welding and thermal cutting recurrent training question bank simulated examination platform operation
Using bindservice method to pause music playing
Danish Technical University pioneered the application of quantum computing to power flow modeling of energy system
Using macro code to generate handwriting automatically in word or WPS
【直播回顾】2022腾讯云未来社区城市运营方招募会暨SaaS 2.0新品发布会!
启动服务11111
Novice, let me show you the "soft test" at one time