Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Company DB
--Sql Server 2014 Express Edition --Batches are separated by 'go' CREATE TABLE employee ( fname varchar(25) not null, minit varchar(1), lname varchar(25) not null, ssn char(9), bdate date, address varchar(50), sex char, salary int, superssn char(9), dno int, primary key (ssn), foreign key (superssn) references employee(ssn) ); CREATE TABLE department ( dname varchar(25) not null, dnumber int, mgrssn char(9) not null, mgrstartdate date, primary key (dnumber), unique (dname), foreign key (mgrssn) references employee(ssn) ); ALTER TABLE employee ADD foreign key (dno) references department(dnumber) ; CREATE TABLE dept_locations ( dnumber int, dlocation varchar(25), primary key (dnumber,dlocation), foreign key (dnumber) references department(dnumber) ); CREATE TABLE project ( pname varchar(25) not null, pnumber int, plocation varchar(25), dnum int not null, primary key (pnumber), unique (pname), foreign key (dnum) references department(dnumber) ); CREATE TABLE works_on ( essn char(9), pno int, hours real, primary key (essn,pno), foreign key (essn) references employee(ssn), foreign key (pno) references project(pnumber) ); CREATE TABLE dependent ( essn char(9), dependent_name varchar(25), sex char, bdate date, relationship varchar(8), primary key (essn,dependent_name), foreign key (essn) references employee(ssn) ); insert into employee values ('James','E','Borg',888665555,'10-NOV-27','450 Stone, Houston, TX','M',55000,null,null); insert into employee values ('Franklin','T','Wong',333445555,'08-DEC-45','638 Voss, Houston, TX','M',40000,888665555,null); insert into employee values ('Jennifer','S','Wallace',987654321,'20-JUN-31','291 Berry, Bellaire, TX','F',43000,888665555,null); insert into employee values ('Jared','D','James',111111100,'10-OCT-1966','123 Peachtree, Atlanta, GA','M',85000,null,null); insert into employee values ('Alex','D','Freed',444444400,'09-OCT-1950','4333 Pillsbury, Milwaukee, WI','M',89000,null,null); insert into employee values ('John','C','James',555555500,'30-JUN-1975','7676 Bloomington, Sacramento, CA','M',81000,null,null); insert into department values ('Research',5,333445555,'22-MAY-1978'); insert into department values ('Administration',4,987654321,'01-JAN-1985'); insert into department values ('Headquarters',1,888665555,'19-JUN-1971'); insert into department values ('Software',6,111111100,'15-MAY-1999'); insert into department values ('Hardware',7,444444400,'15-MAY-1998'); insert into department values ('Sales',8,555555500,'01-JAN-1997'); -- -- Update dno values for managers -- UPDATE employee SET DNO = 5 WHERE ssn = 333445555; UPDATE employee SET DNO = 4 WHERE ssn = 987654321; UPDATE employee SET DNO = 1 WHERE ssn = 888665555; UPDATE employee SET DNO = 6 WHERE ssn = 111111100; UPDATE employee SET DNO = 7 WHERE ssn = 444444400; UPDATE employee SET DNO = 8 WHERE ssn = 555555500; -- -- Insert remaining employees -- insert into employee values ('John','B','Smith',123456789,'09-Jan-55','731 Fondren, Houston, TX','M',30000,333445555,5); insert into employee values ('Alicia','J','Zelaya',999887777,'19-JUL-58','3321 Castle, Spring, TX','F',25000,987654321,4); insert into employee values ('Ramesh','K','Narayan',666884444,'15-SEP-52','971 Fire Oak, Humble, TX','M',38000,333445555,5); insert into employee values ('Joyce','A','English',453453453,'31-JUL-62','5631 Rice, Houston, TX','F',25000,333445555,5); insert into employee values ('Ahmad','V','Jabbar',987987987,'29-MAR-59','980 Dallas, Houston, TX','M',25000,987654321,4); insert into employee values ('Jon','C','Jones',111111101,'14-NOV-1967','111 Allgood, Atlanta, GA','M',45000,111111100,6); insert into employee values ('Justin',null,'Mark',111111102,'12-JAN-1966','2342 May, Atlanta, GA','M',40000,111111100,6); insert into employee values ('Brad','C','Knight',111111103,'13-FEB-1968','176 Main St., Atlanta, GA','M',44000,111111100,6); insert into employee values ('Evan','E','Wallis',222222200,'16-JAN-1958','134 Pelham, Milwaukee, WI','M',92000,null,7); insert into employee values ('Josh','U','Zell',222222201,'22-MAY-1954','266 McGrady, Milwaukee, WI','M',56000,222222200,7); insert into employee values ('Andy','C','Vile',222222202,'21-JUN-1944','1967 Jordan, Milwaukee, WI','M',53000,222222200,7); insert into employee values ('Tom','G','Brand',222222203,'16-DEC-1966','112 Third St, Milwaukee, WI','M',62500,222222200,7); insert into employee values ('Jenny','F','Vos',222222204,'11-NOV-1967','263 Mayberry, Milwaukee, WI','F',61000,222222201,7); insert into employee values ('Chris','A','Carter',222222205,'21-MAR-1960','565 Jordan, Milwaukee, WI','F',43000,222222201,7); insert into employee values ('Kim','C','Grace',333333300,'23-OCT-1970','6677 Mills Ave, Sacramento, CA','F',79000,null,6); insert into employee values ('Jeff','H','Chase',333333301,'07-JAN-1970','145 Bradbury, Sacramento, CA','M',44000,333333300,6); insert into employee values ('Bonnie','S','Bays',444444401,'19-JUN-1956','111 Hollow, Milwaukee, WI','F',70000,444444400,7); insert into employee values ('Alec','C','Best',444444402,'18-JUN-1966','233 Solid, Milwaukee, WI','M',60000,444444400,7); insert into employee values ('Sam','S','Snedden',444444403,'31-JUL-1977','987 Windy St, Milwaukee, WI','M',48000,444444400,7); insert into employee values ('Nandita','K','Ball',555555501,'16-APR-1969','222 Howard, Sacramento, CA','M',62000,555555500,6); insert into employee values ('Bob','B','Bender',666666600,'17-APR-1968','8794 Garfield, Chicago, IL','M',96000,null,8); insert into employee values ('Jill','J','Jarvis',666666601,'14-JAN-1966','6234 Lincoln, Chicago, IL','F',36000,666666600,8); insert into employee values ('Kate','W','King',666666602,'16-APR-1966','1976 Boone Trace, Chicago, IL','F',44000,666666600,8); insert into employee values ('Lyle','G','Leslie',666666603,'09-JUN-1963','417 Hancock Ave, Chicago, IL','M',41000,666666601,8); insert into employee values ('Billie','J','King',666666604,'01-JAN-1960','556 Washington, Chicago, IL','F',38000,666666603,8); insert into employee values ('Jon','A','Kramer',666666605,'22-AUG-1964','1988 Windy Creek, Seattle, WA','M',41500,666666603,8); insert into employee values ('Ray','H','King',666666606,'16-AUG-1949','213 Delk Road, Seattle, WA','M',44500,666666604,8); insert into employee values ('Gerald','D','Small',666666607,'15-MAY-1962','122 Ball Street, Dallas, TX','M',29000,666666602,8); insert into employee values ('Arnold','A','Head',666666608,'19-MAY-1967','233 Spring St, Dallas, TX','M',33000,666666602,8); insert into employee values ('Helga','C','Pataki',666666609,'11-MAR-1969','101 Holyoke St, Dallas, TX','F',32000,666666602,8); insert into employee values ('Naveen','B','Drew',666666610,'23-MAY-1970','198 Elm St, Philadelphia, PA','M',34000,666666607,8); insert into employee values ('Carl','E','Reedy',666666611,'21-JUN-1977','213 Ball St, Philadelphia, PA','M',32000,666666610,8); insert into employee values ('Sammy','G','Hall',666666612,'11-JAN-1970','433 Main Street, Miami, FL','M',37000,666666611,8); insert into employee values ('Red','A','Bacher',666666613,'21-MAY-1980','196 Elm Street, Miami, FL','M',33500,666666612,8); -- -- Insert dept_locations -- insert into dept_locations values (1,'Houston'); insert into dept_locations values (4,'Stafford'); insert into dept_locations values (5,'Bellaire'); insert into dept_locations values (5,'Sugarland'); insert into dept_locations values (5,'Houston'); insert into dept_locations values (6,'Atlanta'); insert into dept_locations values (6,'Sacramento'); insert into dept_locations values (7,'Milwaukee'); insert into dept_locations values (8,'Chicago'); insert into dept_locations values (8,'Dallas'); insert into dept_locations values (8,'Philadephia'); insert into dept_locations values (8,'Seattle'); insert into dept_locations values (8,'Miami'); -- -- Insert project -- insert into project values ('ProductX',1,'Bellaire',5); insert into project values ('ProductY',2,'Sugarland',5); insert into project values ('ProductZ',3,'Houston',5); insert into project values ('Computerization',10,'Stafford',4); insert into project values ('Reorganization',20,'Houston',1); insert into project values ('Newbenefits',30,'Stafford',4); insert into project values ('OperatingSystems',61,'Jacksonville',6); insert into project values ('DatabaseSystems',62,'Birmingham',6); insert into project values ('Middleware',63,'Jackson',6); insert into project values ('InkjetPrinters',91,'Phoenix',7); insert into project values ('LaserPrinters',92,'LasVegas',7); -- -- Insert works_on -- insert into works_on values (123456789,1,32.5); insert into works_on values (123456789,2,7.5); insert into works_on values (666884444,3,40.0); insert into works_on values (453453453,1,20.0); insert into works_on values (453453453,2,20.0); insert into works_on values (333445555,1,10.0); insert into works_on values (333445555,2,10.0); insert into works_on values (333445555,3,10.0); insert into works_on values (333445555,10,10.0); insert into works_on values (333445555,20,10.0); insert into works_on values (333445555,30,10.0); insert into works_on values (333445555,61,10.0); insert into works_on values (333445555,62,10.0); insert into works_on values (333445555,63,10.0); insert into works_on values (333445555,91,10.0); insert into works_on values (333445555,92,10.0); insert into works_on values (999887777,30,30.0); insert into works_on values (999887777,10,10.0); insert into works_on values (987987987,10,35.0); insert into works_on values (987987987,30,5.0); insert into works_on values (987654321,30,20.0); insert into works_on values (987654321,20,15.0); insert into works_on values (888665555,20,0); insert into works_on values (111111100,61,40.0); insert into works_on values (111111101,61,40.0); insert into works_on values (111111102,61,40.0); insert into works_on values (111111103,61,40.0); insert into works_on values (222222200,62,40.0); insert into works_on values (222222201,62,48.0); insert into works_on values (222222202,62,40.0); insert into works_on values (222222203,62,40.0); insert into works_on values (222222204,62,40.0); insert into works_on values (222222205,62,40.0); insert into works_on values (333333300,63,40.0); insert into works_on values (333333301,63,46.0); insert into works_on values (444444400,91,40.0); insert into works_on values (444444401,91,40.0); insert into works_on values (444444402,91,40.0); insert into works_on values (444444403,91,40.0); insert into works_on values (555555500,92,40.0); insert into works_on values (555555501,92,44.0); insert into works_on values (666666601,91,40.0); insert into works_on values (666666603,91,40.0); insert into works_on values (666666604,91,40.0); insert into works_on values (666666605,92,40.0); insert into works_on values (666666606,91,40.0); insert into works_on values (666666607,61,40.0); insert into works_on values (666666608,62,40.0); insert into works_on values (666666609,63,40.0); insert into works_on values (666666610,61,40.0); insert into works_on values (666666611,61,40.0); insert into works_on values (666666612,61,40.0); insert into works_on values (666666613,61,30.0); insert into works_on values (666666613,62,10.0); insert into works_on values (666666613,63,10.0); -- -- Insert dependent -- insert into dependent values (333445555,'Alice','F','05-APR-1976','Daughter'); insert into dependent values (333445555,'Theodore','M','25-OCT-1973','Son'); insert into dependent values (333445555,'Joy','F','03-MAY-1948','Spouse'); insert into dependent values (987654321,'Abner','M','29-FEB-1932','Spouse'); insert into dependent values (123456789,'Michael','M','01-JAN-1978','Son'); insert into dependent values (123456789,'Alice','F','31-DEC-1978','Daughter'); insert into dependent values (123456789,'Elizabeth','F','05-MAY-1957','Spouse'); insert into dependent values (444444400,'Johnny','M','04-APR-1997','Son'); insert into dependent values (444444400,'Tommy','M','07-JUN-1999','Son'); insert into dependent values (444444401,'Chris','M','19-APR-1969','Spouse'); insert into dependent values (444444402,'Alec','M','14-FEB-1964','Spouse'); -- --add your query below
run
|
edit
|
history
|
help
0
VANYA
TESTE PLATAFORMA
Department revenue pivot
aawrish
Cross and outer apply
Demo
Add JSON"Valuetypes to XML Nodes using SQL
COMPUTED COLUMN
QLSV
repert