Create Database and Schema

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

--create database and schema

create database employe


create schema emp

--create employee table

create table emp.employee(emp_id int constraint pk primary key,


emp_name varchar(43)constraint cn not null,
city varchar(23) constraint dn default 'chennai',
email char(23) constraint un unique,
emp_age int constraint ch check(emp_age between 20 and 40))

-- display the structure

sp_help 'emp.employee'

--insert into employee

insert into emp.employee values(32,'nandi',default,'ytrd@cts.com',32)

--display the values

select * from emp.employee


------------------------------------------------------------------------------
-- create department table

create table emp.department(dept_id int identity(100,1),


dept_code int constraint fk references emp.employee(emp_id),
dept_name varchar(23))

--insert values

insert into emp.department values(12,'cse')

--display the values

select * from emp.department

--add one column in department table

alter table emp.department add dept_desc varchar(32)

--modify the value in dept_desc column

update emp.department set dept_desc='comp sci' where dept_code=12

--auto generation off

set identity_insert emp.department on

--manual insert

insert into emp.department (dept_id,dept_code,dept_name,dept_desc)


values (200,11,'cse','comp sci')

-- auto generation on/manual off


set identity_insert emp.department off

--insert the values while in AG ON mode

insert into emp.department values(17,'it','info tech')


---------------------------------------------------------------------------

--Aggregate function

select count(*) from emp.employee


select count(dept_code) dp from emp.department
select max(dept_code) from emp.department
select * from emp.department
select * from emp.employee
select avg(emp_age) EA from emp.employee
select sum(emp_age) EA from emp.employee
------------------------------------------------------------------------------
-- Operation

select * from emp.employee


update emp.employee set emp_age=null where emp_id=16
select emp_id from emp.employee where emp_age is null
select * from emp.employee where emp_age is null
select emp_id from emp.employee where emp_age is not null
select * from emp.employee where emp_age is not null
select emp_name,emp_id from emp.employee where emp_name like '%i%'
select emp_name,emp_id from emp.employee where emp_name not like '%i%'
-----------------------------------------------------------------------------
---
--union/intersect/except

sp_help 'emp.employee'
sp_help 'emp.department'
select emp_id ,emp_name from emp.employee
union
select dept_id,dept_name from emp.department

--intersect

select emp_id from emp.employee


intersect
select dept_code from emp.department

--except

select emp_id from emp.employee


except
select dept_code from emp.department

--group by/order by/having


select * from emp.department
select * from emp.employee
select dept_name,count(dept_name) from emp.department group by dept_name

select dept_name,count(dept_name) from emp.department


group by dept_name order by dept_name desc

select dept_code,count(dept_code) from emp.department


group by dept_code having dept_code=17

select dept_code,count(*) from emp.department where dept_name='it'


group by dept_code

select dept_code,count(*) from emp.department where dept_name='it'


group by dept_code having dept_code=17
-----------------------------------------------------------------------

--Joins
select * from emp.department
select * from emp.employee
--inner
select * from emp.department d
join emp.employee e on e.emp_id=d.dept_code
--full
select * from emp.department d full
join emp.employee e on e.emp_id=d.dept_code
-- left
select * from emp.department d left
join emp.employee e on e.emp_id=d.dept_code
--right
select * from emp.department d right
join emp.employee e on e.emp_id=d.dept_code
--cross // whenever give where condition in cross join,it behaves like inner
join
select * from emp.department d cross
join emp.employee e

select * from emp.department d cross


join emp.employee e where e.emp_id=d.dept_code
-----------------------------------------------------------------------------

--perform constraint operation


select * from emp.department
select * from emp.employee
sp_help 'emp.employee'

--drop constraint

alter table emp.employee


drop constraint un

--add constraint to existing column

alter table emp.employee


add constraint un unique (email)
update emp.employee set email='ias@cts.com' where emp_age=29 --shows some
error
--------------------------------------------------

--VIEWS

create view empdetails


as
select emp_id,emp_name,emp_age from emp.employee

--display the view (empdetails) table

select * from empdetails

--insert the values into view table

insert into empdetails values(34,'indu',28)


update empdetails set emp_age=39 where emp_id=16
delete empdetails where emp_id=34
sp_help 'empdetails'
sp_helptext empdetails

--create and drop the view

create view vv
as select emp_id from emp.employee
select * from vv
drop view vv

--- create view with encryption(to hide the base table details)

create view vvv with encryption


as
select emp_name,city from emp.employee
select * from vvv
sp_helptext 'vvv'

-----------------------------------------------------------------------------

--create ss table

create table emp.ss(id int,name char(34),addr varchar(54))


insert into emp.ss values(2,'indu','eg')
select * from emp.ss
sp_helptext 'emp.ss'

--create view sss from table ss with schemabinding


create view emp.sss with schemabinding
as
select id,addr from emp.ss
select * from emp.sss
insert into emp.sss values(34,'indus')
delete from emp.sss where id=14
--first delete the view then oly can delete the table wen create the view
with schemabinding
drop table emp.ss
drop view emp.sss

You might also like