Bs (Se) F 2019: C T: D S C C: CSS 2062 2019/Comp/AFN/BS (SE) /1925 S B: S T: M - S O
Bs (Se) F 2019: C T: D S C C: CSS 2062 2019/Comp/AFN/BS (SE) /1925 S B: S T: M - S O
You can create a foreign key by defining a FOREIGN KEY constraint when you create or alter a
table. A FOREIGN KEY constraint does not have to be linked only to a PRIMARY KEY constraint in
another table; it can also be defined to reference the columns of a UNIQUE constraint in another
table.
Orders:
Column Name Data Type Size Constraints
Orderid Nchar 5 NOT NULL
Customerid Nchar 5 NOT NULL
Productid Int 4 NOT NULL
OrderDate Datetime - NULL
Create Table Orders
(
OrderId nchar(5) NOT NULL CONSTRAINT order_id PRIMARY
KEY, CustomerId nchar(5) NOT NULL
CONSTRAINT cust_id_fk FOREIGN KEY REFERENCES customers
(customerid), ProductId int NOT NULL,
OrderDate datetime NULL )
CHECK Constraints
CHECK constraints enforce domain integrity by limiting the values that are accepted by a
column. They are similar to FOREIGN KEY constraints in that they control the values that
are placed in a column. The difference is in how they determine which values are valid:
FOREIGN KEY constraints get the list of valid values from another table, and CHECK
constraints determine the valid values from a logical expression that is not based on data
in another column.
For example, it is possible to limit the range of values for a salary column by creating a
CHECK constraint that allows only data that ranges from $15,000 through $100,000. This
prevents salaries from being entered beyond the normal salary range. You can create a
CHECK constraint with any logical (Boolean) expression that returns TRUE or FALSE based
on the logical operators. For the previous example, the logical expression is:
It is possible to apply multiple CHECK constraints to a single column. These are evaluated
in the order in which created. It is also possible to apply a single CHECK constraint to
multiple columns by creating it at the table level.
Employees:
Column Name Data Type Size Constraints
Employeeid Int 4 NOT NULL
LastName Nvarchar 20 NOT NULL
FirstName Nvarchar 20 NOT NULL
Title Nvarchar 10 NULL
BirthDate Datetime 8 NULL
HireDate Datetime 8 NULL
Address Nvarchar 60 NULL
Salary Smallmoney NOT NULL
Note: In the following example, only two columns are added to the employees table
because we are using this example just for demonstration purpose that how a table level
check constraint can be applied, but you have to add other columns shown above as well.
Create Table Employees
(
Employeeid int NOT NULL Constraint emp_pk PRIMARY KEY,
salary smallmoney NOT NULL
CONSTRAINT chk_sal CHECK (salary >= 15000 AND salary <= 100000)
)
ALTER TABLE Employees
ADD CONSTRAINT check_sal
CHECK (salary >= 15000 AND salary <= 100000)
After applying above queries try to perform the following Insert operation on the
Employees table. It will throw error indicating the name of constraint.
DEFAULT definitions
Each column in a record must contain a value, even if that value is NULL. There are
situations when you need to load a row of data into a table but you do not know the value
for a column, or the value does not yet exist. A better solution can be to define a DEFAULT
definition for the column. For example, it is common to specify zero as the default for
numeric columns, or N/A as the default for string columns when no value is specified.
If a column does not allow null values and does not have a DEFAULT definition, you must
specify a value for the column explicitly or SQL Server will return an error indicating that
the column does not allow null values.
You can also explicitly instruct SQL Server to insert the default value for the column using
the DEFAULT VALUES clause of the INSERT STATEMENT.
Following example creates the same orders table with default value assigned to the
orderdate field, which selects the current date from system.
1) Create a table in which you can define primary key, check constraint and default
constraint.
Primary Key:
Check Constraint:
Default Constraint:
2) Now create another table in which use above table’s primary key as foreign key.
Primary Key as Foreign Key:
3) Create a third table in which define composite primary key on any two attributes.
Composite Primary Key:
4) Create a fourth table in which use above table’s composite primary key as foreign
key.