当前位置:网站首页>Bi SQL constraints
Bi SQL constraints
2022-06-23 22:08: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 .
边栏推荐
- v-chart
- Experiment 5 module, package and Library
- Introduction to scikit learn machine learning practice
- HR SaaS is finally on the rise
- API gateway verification token the role of adding a new authentication token in API gateway
- Minimisé lorsque Outlook est allumé + éteint
- Knowda: all in one knowledge mixture model for data augmentation in feed shot NLP
- Kubernetes cluster lossless upgrade practice
- After CVM is configured with IPv6, it cannot be accessed as IPv6 or cannot access IPv6 sites
- Framework not well mastered? Byte technology Daniel refined analysis notes take you to learn systematically
猜你喜欢

万字长文!一文搞懂InheritedWidget 局部刷新机制

Leetcode algorithm interview sprint sorting algorithm theory (32)

《scikit-learn机器学习实战》简介

Error running PyUIC: Cannot start process, the working directory ‘-m PyQt5. uic. pyuic register. ui -o

Bluetooth chip | Renesas and Ti launch new Bluetooth chip, try Lenz st17h65 Bluetooth ble5.2 chip

使用 Provider 改造屎一样的代码,代码量降低了2/3!

嵌入式开发:嵌入式基础——重启和重置的区别

Acl2022 | MVR: multi view document representation for open domain retrieval

从CVPR 2022看域泛化(Domain Generalization)最新研究进展

Selenium batch query athletes' technical grades
随机推荐
v-chart
How do API gateways set up dynamic routing? What are the benefits of dynamic routing?
HDLBits-> Circuits-> Arithmetic Circuitd-> 3-bit binary adder
Analysis of a series a e-commerce app docommandnative
Teacher lihongyi from National Taiwan University - grade Descent 2
Tencent cloud server ubuntu18 installs MySQL and logs in remotely
How to write test cases efficiently?
Error running PyUIC: Cannot start process, the working directory ‘-m PyQt5. uic. pyuic register. ui -o
Raid card with hardware knowledge (5)
Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
Outlook開機自啟+關閉時最小化
Framework not well mastered? Byte technology Daniel refined analysis notes take you to learn systematically
Analysis of Alibaba cloud Tianchi competition -- prediction of o2o coupon
【Proteus仿真】LCD1602+DS1307按键设置简易时钟
The latest research progress of domain generalization from CVPR 2022
How the API gateway obtains the URI path and how the API handles local access failure
DM sub database and sub table DDL "pessimistic coordination" mode introduction - tidb tool sharing
Peking University, University of California Berkeley and others jointly | domain adaptive text classification with structured knowledge from unlabeled data (Domain Adaptive Text Classification Based o
Performance optimization of database 5- database, table and data migration
API gateway verification token the role of adding a new authentication token in API gateway