Вы находитесь на странице: 1из 11

Training Agenda :

Day 2

Constraints
DDL/ DML/ DCL statements
Select Statement Clauses


Constraints:
A constraint is a property that is assigned to a column or a group of columns to
prevent incorrect or corrupt data from entering into the tables. These constraints
ensure the accuracy and reliability of the data into the tables.

There are five type of constraints
1 Primary Key
2 Foreign Key
3 Check
4 Unique
5 Not null
Primary Key:
Primary key is used to uniquely identify a row in a
table. A table can have only one primary key. Primary
keys dont allow null values. The data help across the
primary key column must be unique. It can be defined
at the column level or at the table level.
Primary Key can be defined while creating a table with
Create Table command or it can be added with the
Alter table command.

Table Level:->
CREATE TABLE #Employee(
[Empid] [int] NOT NULL,
CONSTRAINT pk_empID PRIMARY KEY (Empid)
)
Row Level:->
CREATE TABLE #Employee(
[Empid] [int] NOT NULL CONSTRAINT pk_empID PRIMARY KEY

Alter Table #Employee Add constraint Pk_rase Primary Key (Empid)
Alter table #Employee drop constraint Pk_rase



Foreign Key:
This is use to refer the primary key constraint
of master table.
Foreign key allows duplicate and null values.
We can use on delete cascade during the
creation of foreign key so that on the deletion of
primary key records the corresponding foreign
key records get deleted automatically from all
the foreign key tables wherever the primary
key is being followed.


Unique key:
This constraint does not allow
duplicate value but it allows one null value
unlike primary Key which does not allow any
nulls.

Check:
This constraint allows to write any
check condition on the column.

Not null:
This constraint does not allow null
values but allow duplicate values.


T-SQL Languages
DDL (Data Definition Language)
DML (Data Manipulation Language)
DCL (Data Control Language)

DDL:
Using this we can create, alter and drop the
database object

Eg. Create table emp_new
(emp_code smallint
, ename nvarchar(20)
, job nvarchar(20)
, mgr smallint
, hiredate datetime
, sal bigint
, comm smallint
, deptno int)
Applying Constraints SQL Syntax
Eg.
create table account_master
(acc_no int constraint cons_accno_pr_key primary key,
name varchar(30) not null,
acc_type char(12) check (acc_type in (saving, current)),
open_bal numeric check (open_bal >=1000),
email_id varchar (30) constraint email_uniqu unique)

Eg.
create table account_trans
(trans_date datetime,
acc_no int references account_master (acc_no) on
delete cascade,
trans_type char(10) check (trans_type in (Dr, Cr)),
trans_amt int check (trans_amt >0),
curr_bal int check (curr_bal>=1000))
Alter:
alter table emp_new
add email_id nvarchar(50)
alter table emp_new
drop column email_id

Drop:
drop table emp_new


DML :
Using this we can do any kind of
manipulation of record.
Insert
Update
Delete
Select

Insert:
insert into emp_new
(emp_code,ename,job, mgr
, hiredate ,sal,deptno)
Values(7369, SMITH,CLERK
,7902, 12/17/1980,800,20)
Update:
Update emp_new
set
email_id=ename+@microsoft.com
Delete:
delete from emp_new
where emp_code=7369
Select:
select emp_code, ename, job, sal
from emp
Select Statement
The SELECT statement is used to select data from a
Database

Eg.
Select Column1, Column2, Column3
From Table_Name

Вам также может понравиться