Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
267 views

SQL Assignment Employee Database: Submitted By: Komal Ummat L-2010-AE-16-MCA MCA-1 (2 Semester)

The document describes SQL queries performed on employee database tables to create, insert, update, alter and select data from the tables. Tables are created for employee details, department details and salary details. Constraints like primary key, foreign key, unique, check and default are added. Joins, aggregation, filtering and sorting queries are demonstrated.

Uploaded by

Gurpreet Kaur
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
267 views

SQL Assignment Employee Database: Submitted By: Komal Ummat L-2010-AE-16-MCA MCA-1 (2 Semester)

The document describes SQL queries performed on employee database tables to create, insert, update, alter and select data from the tables. Tables are created for employee details, department details and salary details. Constraints like primary key, foreign key, unique, check and default are added. Joins, aggregation, filtering and sorting queries are demonstrated.

Uploaded by

Gurpreet Kaur
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

SQL ASSIGNMENT

EMPLOYEE DATABASE

Submitted By:
Komal Ummat
L-2010-AE-16-MCA
MCA-1(2nd Semester)

create database employee_management

use employee_management

create table emp_details


(
empno int,
empname varchar(20),
empdesig varchar(20),
doj datetime,
empaddress varchar(30),
deptid int
)

create table dept_detail


(
deptid int,
deptname varchar(30)
)

create table salary_details


(
empno int,
leaves int,
presents int,
hra numeric,
pf numeric ,
netsalary numeric
)

insert into emp_details values(23,'H','Manager','12-Jan-2010','122/B.R.S.


Nagar',10)
insert into emp_details values(21,'A','Salesperson','12-June-2010','12/Sarabha
Nagar',15)
insert into emp_details values(12,'B','Accountant','11-March-2010','102/Civil
Lines',20)
insert into emp_details values(15,'I','Executive','10-Jan-2010','23/B.R.S.
Nagar',12)
insert into emp_details values(33,'K','Manager','17-Jan-2011','11/Sarabha
Nagar',5)
insert into emp_details(empno,empname,empdesig,doj,empaddress,deptid)
values(1,'Komal','Manager','14-Jan-2011','154/Sarabha Nagar',5)
insert into emp_details(empno,empname,empdesig,doj,empaddress,deptid)
values(11,'Kiran','Salesperson','13-Jan-2011','154/Civil Lines',15)
insert into emp_details(empno,empname,empdesig,doj,empaddress,deptid)
values(51,'Aayush','Production','13-June-2011','14/Kitchlu Nagar',10)
insert into emp_details(empno,empname,empdesig,doj,empaddress,deptid)
values(34,'Sahil','Accountant','13-July-2011','154/B.R.S.Nagar',12)

Adding a column to the existing table


alter table emp_details add empdor datetime

select * from emp_details

insert into dept_detail values(10,'Production')


insert into dept_detail values(12,'Accounts')
insert into dept_detail values(20,'Marketing')
insert into dept_detail values(5,'Management')
insert into dept_detail values(15,'Sales')

select * from dept_detail


Primary Key Constarint and Not Null Constraint
alter table dept_detail alter column deptid int not null
alter table dept_detail with nocheck add constraint pk primary key(deptid)

View the constarints applied on the table


sp_helpconstraint[dept_detail]

Foreign Key Constraint

alter table emp_details with nocheck add constraint fk foreign key(deptid)


references dept_detail(deptid)

sp_helpconstraint[emp_details]
insert into emp_details(deptid) values(22)

Violation of foreign key constraint


Error Messaage
Msg 547, Level 16, State 0, Line 1
The INSERT statement conflicted with the FOREIGN KEY constraint "fk". The conflict
occurred in database "employee_management", table "dbo.dept_detail", column 'deptid'.
The statement has been terminated.

alter table salary_details add basicpay numeric

insert into salary_details(empno,leaves,presents,hra,pf)


values(21,2,28,1000,2000 )
insert into salary_details(empno,leaves,presents,hra,pf)
values(23,3,27,2000,4000 )
insert into salary_details(empno,leaves,presents,hra,pf)
values(12,1,29,1500,2500 )
insert into salary_details(empno,leaves,presents,hra,pf)
values(33,2,28,3000,4000 )
insert into salary_details(empno,leaves,presents,hra,pf)
values(15,4,26,500,200 )

update salary_details set basicpay=30000 where empno=21


update salary_details set basicpay=60000 where empno=23
update salary_details set basicpay=35000 where empno=12
update salary_details set basicpay=20000 where empno=15
update salary_details set basicpay=55000 where empno=33

update salary_details set netsalary=basicpay+0.25*basicpay-0.085*pf

Check Constraint
alter table salary_details add maritalstatus varchar(4) check
(maritalstatus='Single' or maritalstatus='Married')

update salary_details set maritalstatus='Bachelor' where empno=23

Violation of Check Constarint


Error Message
Msg 547, Level 16, State 0, Line 1
The UPDATE statement conflicted with the CHECK constraint
"CK__salary_de__marit__023D5A04". The conflict occurred in database "employee_management",
table "dbo.salary_details", column 'maritalstatus'.
The statement has been terminated.

Unique Constraint and Default value

create table emp (id int unique,date datetime default '12-Jan-2011')


insert into emp(id) values(1)
select * from emp

insert into emp values(2,'13-Jan-2011')

insert into emp(id) values(1)


Violation of Unique Key constraint

Count(*)
select count(*) from emp_details

Count(columnname)

select count(maritalstatus) from salary_details


RowCount
set rowcount 2
select * from emp_details

set rowcount 0
select * from emp_details

select * from emp_details where deptid=10


select * from emp_details where deptid=10 and empname='K'

select * from emp_details where empname like 'K%'

select * from emp_details where empname like '_o%'

select * from salary_details where netsalary between 43538 and 70000


select * from emp_details where deptid in (10,15)

select * from dept_detail order by deptid

select * from dept_detail order by deptid desc

select * from emp_details order by deptid desc,empname

You might also like