Thursday, 19 November 2015

Sample Create,Insert,Select scripts for DEPT and EMP Tables

-----------------Drop tables if you have already exist in your database------------
if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'emp' AND TABLE_SCHEMA = 'dbo')
    drop table dbo.emp;
   
if exists (select * from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'dept' AND TABLE_SCHEMA = 'dbo')
    drop table dbo.dept;


--------------------------------Create DEPT and Emp tables using below create scripts------------

                                   --***************Dept table**********
create table dept(
  deptno Int,
  dname  varchar(14),
  loc    varchar(13),
  constraint pk_dept primary key (deptno)
);

                                  --**************************Emp table*********************

create table emp(
  empno    Int,
  ename    varchar(10),
  job      varchar(9),
  mgr      int,
  hiredate date,
  sal      int,
  comm     int,
  deptno   int,
  constraint pk_emp primary key (empno),
  constraint fk_deptno foreign key (deptno) references dept (deptno)
);



         ---****************************insert data into Dept table******************************

insert into dept
values(10, 'ACCOUNTING', 'NEW YORK');
insert into dept
values(20, 'RESEARCH', 'DALLAS');
insert into dept
values(30, 'SALES', 'CHICAGO');
insert into dept
values(40, 'OPERATIONS', 'BOSTON');

                     ---********************************Inserting data into emp tables*****************************

insert into emp
values(
 7839, 'KING', 'PRESIDENT', null,
 getdate(),
 5000, null, 10
);
insert into emp
values(
 7698, 'BLAKE', 'MANAGER', 7839,
 getdate()-1,
 2850, null, 30
);
insert into emp
values(
 7782, 'CLARK', 'MANAGER', 7839,
 getdate()-2,
 2450, null, 10
);
insert into emp
values(
 7566, 'JONES', 'MANAGER', 7839,
 getdate()-3,
 2975, null, 20
);
insert into emp
values(
 7788, 'SCOTT', 'ANALYST', 7566,
 getdate()-4,
 3000, null, 20
);
insert into emp
values(
 7902, 'FORD', 'ANALYST', 7566,
 getdate()-4,
 3000, null, 20
);
insert into emp
values(
 7369, 'SMITH', 'CLERK', 7902,
 getdate()-4,
 800, null, 20
);
insert into emp
values(
 7499, 'ALLEN', 'SALESMAN', 7698,
 getdate()-4,
 1600, 300, 30
);
insert into emp
values(
 7521, 'WARD', 'SALESMAN', 7698,
 getdate()-4,
 1250, 500, 30
);
insert into emp
values(
 7654, 'MARTIN', 'SALESMAN', 7698,
 getdate()-4,
 1250, 1400, 30
);
insert into emp
values(
 7844, 'TURNER', 'SALESMAN', 7698,
 getdate()-4,
 1500, 0, 30
);
insert into emp
values(
 7876, 'ADAMS', 'CLERK', 7788,
getdate()-4,
 1100, null, 20
);
insert into emp
values(
 7900, 'JAMES', 'CLERK', 7698,
getdate()-4,
 950, null, 30
);
insert into emp
values(
 7934, 'MILLER', 'CLERK', 7782,
 getdate()-4,
 1300, null, 10
);



select * from dept

select * from emp

No comments:

Post a Comment