If db_id (' DBTest ') is null
Create DATABASE DBTest
Use Eb_fy_data_test---The database context in which the use switch resides
If object_id (' usertest ', ' u ') is not null
Drop TABLE Usertest
CREATE TABLE Usertest {}
1. PRIMARY KEY constraints
ALTER TABLE Usertest
Add Constraint Pk_usertest
Primary Key (ID)
When you create a primary key, SQL Server automatically creates a unique index behind the scenes, guaranteeing uniqueness.
Syntax for adding a unique index (a physical mechanism):
ALTER TABLE Usertest
Add Constraint Pk_usertest
Unique (USERCNO)
2. FOREIGN KEY constraints
ALTER TABLE Usertest
Add Constraint Pk_usertest
Foreign Key (OrderID)
References OrderInfo (OrderID)
You can also add a foreign key constraint to other fields in this table, but you can allow null values at this time
3. Check constraints
ALTER TABLE Usertest
Add Constraint Pk_usertest
Check (age>18)
4. Default constraint is default value
SQL Tech Insider Two DDL