当前位置:网站首页>Bi SQL drop & alter

Bi SQL drop & alter

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

Drop & Alter

Drop, Cancel the operation ;Alter, update operation .

stay PowerBI in , If we modify some data cleaning operations , Just open the advanced editor to view M Language , You can modify and delete specific statements .

But in the database warehouse , We often need the help of Drop and Alter Or other statements to modify the data warehouse .

Why can't we be in a data warehouse like PowerQuery Do the same in ?

In fact, this is related to the positioning of the two .

PowerQuery The positioning of , Is aimed at PowerView To serve , although PowerQuery Self system , It can be called a mini data warehouse , But in general , The data we get from the standard data warehouse are relatively clean data .

SQL Servers The positioning of , It is one of the international basic database languages , Its positioning is a large data warehouse . In the standardized standards of data governance , The data warehouse must be stable 、 Continuously provide data source .

therefore , For the sake of database stability , If we need to modify and adjust the logic of the data warehouse , There must be a set of standardized operation processes and specialized logic modification statements .

grammar

Drop grammar :

1. Delete index :

DROP INDEX  The index name  ON  The name of the table 

2. Delete table :

DROP TABLE  The name of the table 

notes : This operation , Will delete the structure of the table 、 Properties and index !

3. Delete database :

DROP DATABASE  Database name 

4. Keep the table structure , Delete data only :

TRUNCATE TABLE  The name of the table 

Alter grammar :

1. Add columns to the table :

ALTER TABLE  The name of the table 
ADD  Column name   data type 

2. Delete the column in the table :

ALTER TABLE  The name of the table 
DROP COLUMN  Column name 

3. Change the data type of the column in the table :

ALTER TABLE  The name of the table 
ALTER COLUMN  Column name   data type 

Constraints Add

White tea has shared several types of constraints before , In fact, there were additional Drop and Alter Operation of the , Considering the classification problem , White tea decided to put it in this issue .

All the following syntax , When the table has been created , We need to make changes to the constraints .

  • UNIQUE( only ) to update : Add unique constraint :
ALTER TABLE  The name of the table 
ADD UNIQUE ( Column name )

Add multiple column unique constraints :

ALTER TABLE  The name of the table 
ADD CONSTRAINT uc_ Constraint name  UNIQUE ( Column name , Column name ...)

Undo unique constraint :

ALTER TABLE  The name of the table 
DROP CONSTRAINT uc_ Constraint name 
  • PRIMARY KEY( Primary key ) to update :

Add a primary key constraint :

ALTER TABLE  The name of the table 
ADD PRIMARY KEY ( Column name )

Add multiple column primary key constraints :

ALTER TABLE  The name of the table 
ADD CONSTRAINT pk_ Constraint name  PRIMARY KEY ( Column name , Column name ...)

Undo the primary key constraint :

ALTER TABLE  The name of the table 
DROP CONSTRAINT pk_ Constraint name 
  • FOREIGN KEY( Foreign keys ) to update :

New foreign key constraint :

ALTER TABLE  The name of the table 
ADD FOREIGN KEY ( Column name )
REFERENCES  Primary key table ( Column name )

Add multiple column foreign key constraints :

ALTER TABLE  The name of the table 
ADD CONSTRAINT fk_ Constraint name 
FOREIGN KEY ( Column name )
REFERENCES  Primary key table ( Column name )

Undo the foreign key constraint :

ALTER TABLE  The name of the table 
DROP CONSTRAINT fk_ Constraint name 
  • CHECK( Check ) to update :

Add check constraint :

ALTER TABLE  The name of the table 
ADD CHECK ( Column name   Comparison symbol  " Conditions ")

Add multi column check constraint :

ALTER TABLE  The name of the table 
ADD CONSTRAINT chk_ Constraint name  CHECK ( Column name   Comparison symbol  " Conditions " AND  Column name   Comparison symbol  " Conditions " ......)

Undo check constraint :

ALTER TABLE  The name of the table 
DROP CONSTRAINT chk_ Constraint name 
  • DEFAULT( The default value is ) to update :

New default value constraint :

ALTER TABLE  The name of the table 
ALTER COLUMN  Column name  SET DEFAULT ' The default value is '

Undo the default value constraint :

ALTER TABLE  The name of the table 
ALTER COLUMN  Column name  DROP DEFAULT

That's the end of this issue , No use cases , Because we do BI In fact, it is rare for the front end to use such operations , We are more simply using query aggregation statements .

Of course , Yes ETL And interested partners of the data warehouse can have a try .

This is white tea , One PowerBI Beginners .

原网站

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