Commit 24011613 authored by michael lundquist's avatar michael lundquist
Browse files

week2 hw

parent 5d5ce6fa
/*
<a href="https://stackoverflow.com/questions/32688805/practical-difference-between-pls-and-sql-file-in-oracle">
file extensions</a>
*/
/*
problem 1:
Create a new table using the CREATE TABLE command.
Use your first name and your last name for the name of the table
(for instance, for John Smith the name of the table will be JOHN_SMITH).
Make sure to include at least four different data types
(CHAR, VARCHAR2, NUMBER, DATE) for the columns. Use the
DESCRIBE command to verify that the columns have been defined correctly.
*/
CREATE TABLE MICHAEL_LUNDQUIST (ID integer, firstName
VARCHAR2(20), lastName VARCHAR2(20), addDay DATE DEFAULT SYSDATE, zip CHAR(5));
/*
problem 2:
Obtain the columns names and data types of the view USER_TABLES using
the DESCRIBE command. Retrieve the names of all the tables of a user’s
database tables using the SELECT table_name FROM user_tables; command.
*/
--michael lundquist 00737340
DESC USER_TABLES;
--michael lundquist 00737340
SELECT table_name FROM user_tables;
/*
problem 3:
Use the ALTER TABLE … ADD command to add a column to the table
created in Problem 1.
*/
--michael lundquist 00737340
ALTER TABLE MICHAEL_LUNDQUIST ADD userName VARCHAR2(20);
/*
problem 4:
Use the ALTER TABLE … MODIFY command to change the size of any column
in the table created in Problem 1.
*/
--MICHAEL LUNDQUIST 00737340
ALTER TABLE MICHAEL_LUNDQUIST MODIFY (lastName VARCHAR2(30));
/*
problem 5:
Use the ALTER TABLE … DROP COLUMN command to drop a column in the
table created in Problem 1.
*/
--MICHAEL LUNDQUIST 00737340
ALTER TABLE MICHAEL_LUNDQUIST DROP COLUMN ZIP;
/*
problem 6
Rename the table created in Problem 1 using the RENAME … TO command.
*/
--MICHAEL LUNDQUIST 00737340
RENAME MICHAEL_LUNDQUIST TO HELLO;
/*
problem 7
Drop the table created in Problem 1 using the DROP TABLE command.
*/
DROP TABLE HELLO;
/*
problem 8
Restore the table created in Problem 1 using the FLASHBACK TABLE command.
*/
--MICHAEL LUNDQUIST 00737340
FLASHBACK TABLE HELLO TO BEFORE DROP;
/*
problem 9
Use the DROP TABLE … PURGE command to delete your table permanently.
Use the SELECT object_name, original_name FROM recyclebin;
command to confirm that the table is not in the recycle bin anymore.
*/
--MICAHEL LUNDQUIST 00737340
DROP TABLE HELLO PURGE;
\ No newline at end of file
Practice Problems: Table Creation and Management
When working on practice problems, follow examples shown in the Ppt presentation but do not copy their solutions unless it is the only possible answer to the problem.
It is recommended to type an SQL query in a Notepad file first and then copy the command into SQL*Plus. This way, if you make a mistake you can easily go back to your command and correct it. If you need help, please feel free to reference the Practice Problems Hints file posted below.
Practice Problems:
Create a new table using the CREATE TABLE command. Use your first name and your last name for the name of the table (for instance, for John Smith the name of the table will be JOHN_SMITH). Make sure to include at least four different data types (CHAR, VARCHAR2, NUMBER, DATE) for the columns. Use the DESCRIBE command to verify that the columns have been defined correctly.
Obtain the columns names and data types of the view USER_TABLES using the DESCRIBE command. Retrieve the names of all the tables of a user’s database tables using the SELECT table_name FROM user_tables; command.
Use the ALTER TABLE … ADD command to add a column to the table created in Problem 1.
Use the ALTER TABLE … MODIFY command to change the size of any column in the table created in Problem 1.
Use the ALTER TABLE … DROP COLUMN command to drop a column in the table created in Problem 1.
Rename the table created in Problem 1 using the RENAME … TO command.
Drop the table created in Problem 1 using the DROP TABLE command.
Restore the table created in Problem 1 using the FLASHBACK TABLE command.
Use the DROP TABLE … PURGE command to delete your table permanently. Use the SELECT object_name, original_name FROM recyclebin; command to confirm that the table is not in the recycle bin anymore.
Instructions: For each problem
write and execute an SQL query in SQL*Plus
verify the result and type your name at the following SQL prompt
take a screen shot that includes the SQL statement, the result, and your name
copy and paste the screen shot into a MS Word file
When all problems are completed, type your name and course number in that file, and submit it through Bb.
Submission: The submission link will appear automatically once all quizzes are completed successfully.
Due date: See the Schedule of Classes for the due date.
\ No newline at end of file
/*
prob1:
STUDENT table
*/
CREATE TABLE STUDENT
( ST_NUMBER NUMBER(6) PRIMARY KEY
, ST_LAST VARCHAR2(35) NOT NULL
, ST_FIRST VARCHAR2(25)
, ST_DOB DATE );
/*
PROB2:
VENDOR
*/
CREATE TABLE VENDOR
(VEND_NUMBER NUMBER(5) PRIMARY KEY
, VEND_NAME VARCHAR2(35) NOT NULL);
/*
PROB3:
PRODUCT
*/
CREATE TABLE PRODUCT
(PROD_CODE NUMBER(5) PRIMARY KEY
, PROD_DESC VARCHAR2(35));
/*
PROB4:
PRODUCT *--* VENDOR BRIDGE
ALTER TABLE tablename
ADD [CONSTRAINT constraint] FOREIGN KEY (fkcolumnname)
REFERENCES referencedtablename (referencedcolumnname);
*/
CREATE TABLE SELLS
(PROD_CODE NUMBER(5),
VEND_NUMBER NUMBER(5),
CONSTRAINT VENDOR_VEND_NUMBER_fk
FOREIGN KEY (VEND_NUMBER)
REFERENCES VENDOR (VEND_NUMBER),
CONSTRAINT PRODUCT_PROD_CODE_fk
FOREIGN KEY (PROD_CODE)
REFERENCES PRODUCT (PROD_CODE));
select * from all_constraints where r_constraint_name in (select constraint_name
from all_constraints where table_name='SELLS');
select constraint_name, constraint_type
from user_constraints
where table_name = 'SELL';
/*
problem 5
Use the ALTER TABLE … ADD CONSTRAINT command to
add the UNIQUE constraint to the PROD_DESC column in the PRODUCT table.
*/
ALTER TABLE PRODUCT ADD CONSTRAINT PRODUCT_PROD_DESC_UK UNIQUE (PROD_DESC);
select constraint_name, constraint_type
from user_constraints
where table_name = 'PRODUCT';
/*
PROBLEM 6
Use the ALTER TABLE … ADD CONSTRAINT command to add the CHECK
constraint to the ST_DOB column
to verify that all dates of birth are before 01/01/2000.
*/
ALTER TABLE STUDENT ADD CONSTRAINT STUDENT_ST_DOB_ck CHECK (TO_DATE('01-01-2000', 'DD-MM-YYYY') > ST_DOB);
INSERT INTO STUDENT (ST_NUMBER, ST_FIRST, ST_LAST, ST_DOB) VALUES (1, 'MICHAEL', 'LUNDQUIST', TO_DATE('16-07-2003', 'DD-MM-YYYY'))
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment