Saturday, 11 August 2012

use of constraint key word and select record from table with condition


Use of constraint key word and selection of  record from table with condition 

--first create database………………………..
create database databasename
--select database …..
use databasename



--creating table with CONSTRAINT keyword record entry. SECOND COLUMN value will be  either ‘f’ or ‘m’ . if user enter exempt ‘f’ or ‘m’ the sql server generate error.  
create table stu_gen (roll int primary key IDENTITY(1,1), gen nvarchar(1) CONSTRAINT gen_CONSTRAINT CHECK (gen IN ('f','m')))

--INSERT RECORD IN STU_GEN TABLE
insert into stu_gen(gen) values('f')
insert into stu_gen(gen) values('m')
insert into stu_gen(gen) values('f')
insert into stu_gen(gen) values('m')
insert into stu_gen(gen) values('f')
insert into stu_gen(gen) values('f')
insert into stu_gen(gen) values('m')
insert into stu_gen(gen) values('f')
--RECORD SELECTION WITH CONDITION
 select roll,Gender=(select Gender= case when
stu_gen.gen='m' then 'Male'
else
'Female'
end)
from stu_gen

0 comments: