Answers to SQL Practice Questions

Telechargé par Alichaabane98
Answers to SQL Practice Questions
ANSWERS: (A) Create/Alter/Drop Table Commands:
CREATE TABLE EMP
(emp_num int ,
emp_fname char(20) WITH DEFAULT 'unknown' ,
emp_lname char(20) NOT NULL ,
job_class char(3) ,
hiredate date NOT NULL ,
CONSTRAINT emp_pk PRIMARY KEY (emp_num) ,
CONSTRAINT job_fk FOREIGN KEY (job_class) REFERENCES job (job_code)
CONSTRAINT emp_lname_uk UNIQUE ( emp_lname ) ,
CONSTRAINT hiredate_ck CHECK ( hiredate >= '1/1/1990' )
)
2. CREATE TABLE JOB (
job_code char( 3 ) ,
job_class char( 30 ) ,
chg_hour decimal (5,2) WITH DEFAULT 15 ,
CONSTRAINT job_pk PRIMARY KEY (job_code) ,
CONSTRAINT job_class_ck CHECK ( chg_hour >= 15 )
)
3. ALTER TABLE job
ADD COLUMN
overtimeCharge dec (6,2) NOT NULL
4. ALTER TABLE job
ADD CONSTRAINT
charge_ck CHECK( chg_hour >= 25 )
5. ALTER TABLE job
ALTER column
Job_Class set default 'NA'
6. ALTER TABLE job
ALTER column
Chg_Hour set NOT NULL
7. ALTER TABLE job
ALTER column
Chg_Hour set data type decimal (12,2)
8. ALTER TABLE job
DROP constraint charge_ck
9. ALTER TABLE job
DROP COLUMN overtimeCharge
10. ALTER TABLE job
DROP COLUMN chg_hour
11. DROP TABLE job
12. DROP TABLE emp
ANSWERS: (B) Data Manipulation Commands:
1. Insert the last 3 rows shown into the EMP table.
INSERT into emp ( emp_num, emp_fname, emp_lname, job_class, hiredate )
VALUES
( 106, 'William', 'Smithfield', 'PRG', '10/31/2002' ),
INSERT into emp ( emp_num, emp_fname, emp_lname )
VALUES
( 114, 'Annelise', 'Jones' )
INSERT into emp ( emp_num, emp_lname )
VALUES ( 118, 'Frommer' )
2. Update the employee fname to Jim instead of James for employee 118
(Note: if no WHERE clause is used, then all records are updated)
UPDATE emp
SET emp_fname = 'Jim'
WHERE emp_num = 118
3. Update the Database Designer chg_hour value by 10%
UPDATE job
SET chg_hour = chg_hour * 1.1
WHERE job_class = 'Database Designer'
4. Remove the row from the job table for job code PRG
( Note: if no WHERE clause is used, then all records are deleted )
DELETE
FROM job
WHERE job_code = 'PRG'
5. Remove the JOB table and all of it's data
DROP TABLE job
6. Remove the employees from the employee table WHERE the hiredate is before 1-Jan-90
DELETE
FROM emp
WHERE hiredate < '1-Jan-90'
7. Remove the emp table and all of it's data
DROP TABLE emp
ANSWER: (C) Select Commands:
1. Show the employee first and last names and their job class description and hourly charge.
SELECT emp.emp_fname ,
emp.emp_lname ,
job.job_class ,
job.chg_hour
FROM emp, job
WHERE emp.job_class = job.job_code
2. Same question as above, but only show the records WHERE the chg_hour is <100
SELECT emp.emp_fname ,
emp.emp_lname ,
job.job_class ,
job.chg_hour
FROM emp, job
WHERE emp.job_class = job.job_code
AND job.chg_hour = < 100
3. Select the hours worked and lastname and hiredate for all employees.
(Note: the default for all selects is to show all records that are joined. The above statement has no
further conditions for the WHERE clause)
SELECT emp.emp_lname ,
emp.hiredate ,
proj_emp.hour
FROM emp, proj_emp
WHERE emp.emp_num = proj_emp.emp_num
1 / 20 100%

Answers to SQL Practice Questions

Telechargé par Alichaabane98
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans linterface ou les textes ? Ou savez-vous comment améliorer linterface utilisateur de StudyLib ? Nhésitez pas à envoyer vos suggestions. Cest très important pour nous !