0% found this document useful (0 votes)
74 views

Not Null: Alter Table Add Constraint Foreign Key References

To create a database, alter a database name or delete a database using queries. To add or drop constraints like primary keys, foreign keys, defaults and checks. The document provides SQL statements to create a database and table, add a foreign key between two tables, add or drop default, check and foreign key constraints on an existing table.

Uploaded by

KSul2012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Not Null: Alter Table Add Constraint Foreign Key References

To create a database, alter a database name or delete a database using queries. To add or drop constraints like primary keys, foreign keys, defaults and checks. The document provides SQL statements to create a database and table, add a foreign key between two tables, add or drop default, check and foreign key constraints on an existing table.

Uploaded by

KSul2012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

To Create the database using a query

Create database DatabaseName


To alter a database, once it's created
Alter database DatabaseName Modify Name = NewDatabaseName
Alternatively, you can also use system stored procedure
Execute sp_renameDB 'OldDatabaseName','NewDatabaseName'
To Delete or Drop a database
Drop Database DatabaseThatYouWantToDrop
Create Table tblGender
(ID int Not Null Primary Key,
Gender nvarchar(50))
To add a foreign key reference using a query
Alter table tblPerson
add constraint tblPerson_GenderId_FK FOREIGN KEY (GenderId) referencestblGender(ID)
The general formula is here
Alter table ForeignKeyTable add constraintForeignKeyTable_ForiegnKeyColumn_FK
FOREIGN KEY (ForiegnKeyColumn) references PrimaryKeyTable (PrimaryKeyColumn)
Altering an existing column to add a default constraint:
ALTER TABLE { TABLE_NAME }
ADD CONSTRAINT { CONSTRAINT_NAME }
DEFAULT { DEFAULT_VALUE } FOR { EXISTING_COLUMN_NAME }
Adding a new column, with default value, to an existing table:
ALTER TABLE { TABLE_NAME }
ADD { COLUMN_NAME } { DATA_TYPE } { NULL | NOT NULL }
CONSTRAINT { CONSTRAINT_NAME } DEFAULT { DEFAULT_VALUE }
The following command will add a default constraint, DF_tblPerson_GenderId.
ALTER TABLE tblPerson
ADD CONSTRAINT DF_tblPerson_GenderId
DEFAULT 1 FOR GenderId
The following check constraint, limits the age between ZERO and 150.
ALTER TABLE tblPerson
ADD CONSTRAINT CK_tblPerson_Age CHECK (Age > 0 AND Age < 150)
The general formula for adding check constraint in SQL Server:
ALTER TABLE { TABLE_NAME }
ADD CONSTRAINT { CONSTRAINT_NAME } CHECK ( BOOLEAN_EXPRESSION )
To drop the CHECK constraint:
ALTER TABLE tblPerson
DROP CONSTRAINT CK_tblPerson_Age

You might also like