Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For each of the steps below you may create a separate SQL script file and SPOOL

ID: 3856060 • Letter: F

Question

For each of the steps below you may create a separate SQL script file and SPOOL file or you may want to put the SPOOL output for several steps, from the same SQL script file, in the same file. Be sure your SPOOL file(s) contains your SQL statements along with the Oracle responses and/or displayed

results. Do NOT submit your SQL script files. Only submit your output SPOOL files.

Assignment Details:

Create Oracle database tables using SQL Data Definition Language (DDL) for each table listed in the metadata of Assignment 1. You may need to use a combination of DROP TABLE, CREATE TABLE, and ALTER TABLE SQL statements. Make sure that entity and referential integrity are enforced by declaring a primary key for each table (these may be composite keys) and declaring all appropriate foreign keys. Your CREATE TABLE and ALTER TABLE statements (if desired) must show integrity constraints, as appropriate, for NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, REFERENCES, and CHECK constraints. Be sure to save your SQL script file used to create these tables with a .sql extension and your output SPOOL file with a

.lst or .txt extension. You should rerun and test your SQL script file until it runs without any errors (this is why you’ll want to include DROP TABLE statements). Submit your SPOOL file showing that all SQL in your SQL script file worked properly.

Populate each of your tables with at least five valid rows of data each and show the SQL INSERT statements as you executed them. Populate other tables in your database, as necessary, to satisfy referential integrity. Save your SQL script file and SPOOL file with the correct extensions. You should test and rerun your SQL script file until it runs without any error. Submit your SPOOL file showing that all SQL in your SQL script file worked properly.

Develop an SQL script file to perform the following queries and updates. You should test your SQL script file until it runs without any errors.

Retrieve all of your customers' names, account numbers, and addresses (street and zip code only), sorted by account number.

Retrieve all of the videos rented in the last 30 days and sort in chronological rental date order.

Produce a list of your distributors and all their information sorted in order by company name.

Update a customer name to change their maiden name to a married name. You can choose which row to update. Make sure that you use the primary key column in your WHERE clause to affect only a specific row. You may want to include a ROLLBACK statement to undo your data update.

Delete a customer from the database. You can choose which row to delete. Make sure that you use the primary key column in your WHERE clause to affect only a specific row. You may want to include a ROLLBACK statement to undo your data deletion.

Submit your SPOOL file(s) showing that all SQL in your SQL script file worked properly. Show the actual SQL statements executed and the results the SQL produced below the code by making sure that you have a SET ECHO STATEMENT in your SQL script file(s).

Explanation / Answer

To create a table, you can give the following statement

create table emp (empno number(5) primary key,
                   name varchar2(20),
                   sal number(10,2),
                   job varchar2(20),
                   mgr number(5),
                   Hiredate date,
                   comm number(10,2));

create table tax (empno number(5), tax number(10,2));

insert into tax select empno,(sal-5000)*0.40
                     from emp where sal > 5000;

create table tax as select empno,(sal-5000)*0.4
   as tax from emp where sal>5000

create table emp2 as select * from emp;

create table emp2 as select * from emp where 1=2;

CREATE GLOBAL TEMPORARY TABLE taxable_emp
        (empno number(5),
          ename varchar2(20),
          sal   number(10,2),
          tax   number(10,2))
      ON COMMIT DELETE ROWS;

Use the ALTER TABLE statement to alter the structure of a table.

alter table emp add (addr varchar2(20), city varchar2(20),
      pin varchar2(10),ph varchar2(20));

alter table emp modify (ename varchar2(15));

rename emp2 to employee2

Use the drop statement to drop tables, functions, procedures, packages, views, synonym, sequences, tablespaces etc.

Example

The following command drops table emp2

drop table emp2;