0% found this document useful (0 votes)
43 views11 pages

Practica3-2 lsbdII 2021 2

This document outlines a practice assignment on Oracle DDL statements for a Laboratory of Database Systems II course. It provides background on the assignment such as the academic program, subject, and due dates. It describes the objective of learning Oracle DDL statements and references materials. It lists initial and follow up activities for students to complete that involve categorizing database objects, listing data types, creating tables, exploring table and object structures using SQL statements, and modifying and dropping tables. Screenshots are to be included.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views11 pages

Practica3-2 lsbdII 2021 2

This document outlines a practice assignment on Oracle DDL statements for a Laboratory of Database Systems II course. It provides background on the assignment such as the academic program, subject, and due dates. It describes the objective of learning Oracle DDL statements and references materials. It lists initial and follow up activities for students to complete that involve categorizing database objects, listing data types, creating tables, exploring table and object structures using SQL statements, and modifying and dropping tables. Screenshots are to be included.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Universidad Autónoma de Zacatecas

Unidad Académica de Ingeniería Eléctrica


Programa Académico de Ingeniería de Software

Practice 2

Practice name DDL


Academic Program Software Engineering
Subject name Laboratory of Database Systems II
Unit I. SQL.

Professor Aldonso Becerra Sánchez

Due date August 27, 2020


Due date with penalty August 28, 2020
Elaboration date August 24, 2020

Practice objective Learn the complete notion of Oracle DDL statements.


Estimated time of
6 hours
completion
The Oracle DDL language is transcendental in the handling of
SQL statements at the level of both administrator and database
Introduction
programmer, since it allows the definition of database schemes
regardless of the platform used to generate it.

Reference 1:
1. Oracle Database 11g: SQL Fundamentals.

Reference 2:
2. Oracle Database SQL Language Reference 11g.

Reference 3:

Initial activity:
Read the entire file first. Write the corresponding report. Start with the Introduction
section.

Activity 1:
Write the section that describes the Work developed in the following activities.

Página 1 de 11
Practice 2
Universidad Autónoma de Zacatecas
Unidad Académica de Ingeniería Eléctrica
Programa Académico de Ingeniería de Software

Read all the choices carefully because there might be more than one correct answer.
Choose all the correct answers for each question. If necessary, investigate some answers.
Explain the reason for your answer.
Categorize the Main Database Objects
1. If a table is created without specifying a schema, in which schema will it be?
(Choose the best answer). Challenge question.
A. It will be an orphaned table, without a schema.
B. The creation will fail.
C. It will be in the SYS schema.
D. It will be in the schema of the user creating it.
E. It will be in the PUBLIC schema.
2. Several object types share the same namespace, and therefore cannot have the
same name in the same schema. Which of the following object types is not in the
same namespace as the others? (Choose the best answer). Challenge question.
A. Index
B. PL/SQL stored procedure
C. Synonym
D. Table
E. View
3. Which of these statements will fail because the table name is not legal? (Choose
two answers.)
A. create table “WHERE” (col1 date);
B. create table “mincase” (col1 date);
C. create table 1var (col1 date);
D. create table var1 (col1 date);
E. create table delete (col1 date);
List the Data Types that Are Available for Columns
4. Which of the following data types are variable length? (Choose all correct
answers.)
A. BLOB
B. CHAR
C. LONG
D. NUMBER
E. RAW
F. VARCHAR2
5. Study these statements:
create table tab1 (c1 number(1), c2 date);
alter session set nls_date_format='dd-mm-yy';
insert into tab1 values (2.2,’29-07-09’);
Will the insert succeed? (Choose the best answer)
A. The insert will fail because the 2.2 is too long.
Página 2 de 11
Practice 2
Universidad Autónoma de Zacatecas
Unidad Académica de Ingeniería Eléctrica
Programa Académico de Ingeniería de Software

B. The insert will fail because the ’29-07-09’ is a string, not a date.
C. The insert will fail for both reasons A and B.
D. The insert will succeed.
6. Which of the following is not supported by Oracle as an internal data type?
(Choose the best answer.)
A. CHAR
B. FLOAT
C. INTEGER
D. STRING
Create a Simple Table
7. Consider this statement:
create table t1 as select * from employees where 9=4;
What will be the result? (Choose the best answer.)
A. There will be an error because of the impossible condition.
B. No table will be created because the condition returns FALSE.
C. The table T1 will be created but no rows inserted because the condition returns
FALSE.
D. The table T1 will be created and every row in EMPLOYEES inserted because the
condition returns a NULL as a row filter.
8. When a table is created with a statement such as the following:
create table newtable as select * from oldtable; will there be any constraints on the
new table? (Choose the best answer.)
A. The new table will have no constraints, because constraints are not copied when
creating tables with a subquery.
B. All the constraints on TAB will be copied to NEWTABLE.
C. Primary key and unique constraints will be copied but not check and not null
constraints.
D. Check and not null constraints will be copied but not unique or primary key.
E. All constraints will be copied, except foreign key constraints.
Explain How Constraints Are Created at the Time of Table Creation
9. Which types of constraint require an index? (Choose all that apply.)
A. CHECK
B. NOT NULL
C. PRIMARY KEY
D. UNIQUE
10. A transaction consists of two statements. The first succeeds, but the second
(which updates several rows) fails partway through because of a constraint
violation. What will happen? (Choose the best answer). Challenge question.
A. The whole transaction will be rolled back.
B. The second statement will be rolled back completely, and the first will be committed.
C. The second statement will be rolled back completely, and the first will remain
uncommitted.
Página 3 de 11
Practice 2
Universidad Autónoma de Zacatecas
Unidad Académica de Ingeniería Eléctrica
Programa Académico de Ingeniería de Software

D. Only the one update that caused the violation will be rolled back; everything else will
be committed.
E. Only the one update that caused the violation will be rolled back; everything else will
remain uncommitted.
Activity 2:
Write the section that describes the Work developed in the following activities.

EXECUTE THE FOLLOWING SENTENCES. INCLUDE AN OUTPUT IMAGE


FOR EACH ONE.

Determine What Objects Are Accessible to Your Session


In this exercise, query various data dictionary views as user HR to determine what objects
are in the HR schema and what objects in other schemas HR has access to.
1. Connect to the database with SQL Developer as user HR.
2. Determine how many objects of each type are in the HR schema:
select object_type,count(*) from user_objects group by object_type;
The USER_OBJECTS view lists all objects owned by the schema to which the current
session is connected, in this case HR.
3. Determine how many objects in total HR has permissions on:
select object_type,count(*) from all_objects group by object_type;
The ALL_OBJECTS view lists all objects to which the user has some sort of access.
4. Determine who owns the objects HR can see:
select distinct owner from all_objects;

Investigate Table Structures


In this exercise, query various data dictionary views as user HR to determine the structure
of a table.
1. Connect to the database with SQL Developer as user HR.
2. Determine the names and types of tables that exist in the HR schema:
select table_name,cluster_name,iot_type from user_tables;
Clustered tables and index organized tables (IOTs) are advanced table structures. In the
HR schema, all tables are standard heap tables except for COUNTRIES which is an IOT.
3. Use the DESCRIBE command to display the structure of a table:
describe regions;
4. Retrieve similar information by querying a data dictionary view:
select column_name,data_type,nullable from user_tab_columns
where table_name='REGIONS';

Investigate the Data Types in the HR schema


In this exercise, find out what data types are used in the tables in the HR schema, using
two techniques.
Página 4 de 11
Practice 2
Universidad Autónoma de Zacatecas
Unidad Académica de Ingeniería Eléctrica
Programa Académico de Ingeniería de Software

1. Connect to the database as user HR with SQL Developer.


2. Use the DESCRIBE command to show the data types in some tables:
describe employees;
describe departments;
3. Use a query against a data dictionary view to show what columns make up the
EMPLOYEES table, as the DESCRIBE command would:
select column_name,data_type,nullable,data_length,data_precision, data_scale from
user_tab_columns where table_name='EMPLOYEES';

Create Tables
In this exercise, use SQL Developer to create a heap table, insert some rows with a
subquery, and modify the table. Do some more modifications with SQL*Plus, then drop
the table.
1. Connect to the database as user HR with SQL Developer.
2. Right-click the Tables branch of the navigation tree, and click New Table.
3. Name the new table EMPS, and use the Add Column button to set it up as in the
following illustration:

4. Click the DDL tab to see if the statement that has been constructed. It should look like
this:
CREATE TABLE EMPS(
EMPNO NUMBER,
ENAME VARCHAR2(25),
Página 5 de 11
Practice 2
Universidad Autónoma de Zacatecas
Unidad Académica de Ingeniería Eléctrica
Programa Académico de Ingeniería de Software

SALARY NUMBER,
DEPTNO NUMBER(4, 0));
Return to the Table tab (as in the preceding illustration) and click OK to create the table.
5. Run this statement:
insert into emps select employee_id,last_name,salary,department_id
from employees;
and commit the insert:
commit;
6. Right-click the EMPS table in the SQL Developer navigator, click Column and Add.
7. Define a new column HIRED, type DATE, as in the following illustration below; and
click Apply to create the column.

8. Connect to the database as HR.


9. Define a default for HIRED column in the EMPS table:
alter table emps modify (hired default sysdate);
10. Insert a row without specifying a value for HIRED and check that the new row does
have a HIRED date but that the other rows do not:
insert into emps (empno,ename) values(99,'Newman');
select hired,count(1) from emps group by hired;
11. Tidy up by dropping the new table:
drop table emps;

Página 6 de 11
Practice 2
Universidad Autónoma de Zacatecas
Unidad Académica de Ingeniería Eléctrica
Programa Académico de Ingeniería de Software

Work with Constraints


Use SQL Developer to create tables, add constraints, and demonstrate their use.
1. Connect to the database as user HR.
2. Create a table EMP as a copy of some columns from EMPLOYEES:
create table emp as
select employee_id empno, last_name ename, department_id deptno
from employees;
3. Create a table DEPT as a copy of some columns from DEPARTMENTS:
create table dept as
select department_id deptno, department_name dname from departments;
4. Use DESCRIBE to describe the structure of the new tables. Note that the not null
constraint on ENAME and DNAME has been carried over from the source tables.
5. Add a primary key constraint to EMP and to DEPT and a foreign key constraint linking
the tables:
alter table emp add constraint emp_pk primary key (empno);
alter table dept add constraint dept_pk primary key (deptno);
alter table emp add constraint
dept_fk foreign key (deptno) references dept on delete set null;
The preceding last constraint does not specify which column of DEPT to reference; this
will default to the primary key column.
6. Demonstrate the effectiveness of the constraints by trying to insert data that will violate
them:
insert into dept values(10,'New Department');
insert into emp values(9999,'New emp',99);
truncate table dept;
7. Tidy up by dropping the tables. Note that this must be done in the correct order:
drop table emp;
drop table dept;
Activity 3:
Write the section that describes the Work developed in the following activities.

Consider this simple analysis of a call record system in a local telephone company:
A subscriber is identified by a customer number and also has a name, last name, date of
birth, address, rfc, 1 or 2 references (friends or relatives) and possibly one or more
telephones. A telephone is identified by its number, which must be a 10-digit integer
beginning with 55, an activation date, and a flag (‘A’ or ‘I’) for whether it is active.
Inactive telephones are not assigned to a subscriber; active telephones are. These
subscribers are associated to physical telephones, and also have a brand, capacity
(memory, cpu, display, camera, …) and serial number. Besides, all telephones are
engaged with a forced plan: 6, 12, 18 or 24 months. It is necessary to store the start and

Página 7 de 11
Practice 2
Universidad Autónoma de Zacatecas
Unidad Académica de Ingeniería Eléctrica
Programa Académico de Ingeniería de Software

final date of it (when this is hired). For every call, it is necessary to record the time it
started and the time it finished.

• Create the necessary tables.


• Generate the constraints and defaults that can be used to implement this system.
• Generate the corresponding relational model using SQL Data Modeler (dragging
the tables using GUI).
• Generate the corresponding logical model using SQL Data Modeler (reverse
engineering).

Activity 4:
Write the section that describes the Work developed in the following activities.

Propose a response to the following scenario issue:


• You are designing table structures for a human resources application. The
business analysts have said that when an employee leaves the company, his
employee record should be moved to an archive table. Can constraints help?
Explain the reasons.

Activity 5:
Write the section that describes the Work developed in the following activities.

Carry out the following steps (capture an image for each statement output):
• Create the EMPLOYEES_2 table based on the EMPLOYEES table from HR
scheme.
Use the CREATE statement that employs a SELECT statement.
• Describe the table structure.
• Alter the EMPLOYEES_2 table status to read-only.
• Try to insert a row the table. Depict the results.
• Revert the EMPLOYEES_2 table to the write status. Now, try to insert the same
row again. Depict the results.
• Drop the EMPLOYEES_ table.

Activity 6:
Write the section that describes the Work developed in the following activities.

Taking into account the following diagram.

Página 8 de 11
Practice 2
Universidad Autónoma de Zacatecas
Unidad Académica de Ingeniería Eléctrica
Programa Académico de Ingeniería de Software

1. Generate the DDL statements to create only the isolated tables of


Product_Hierarchy and Product_Suppliers (add constraints here in CREATE,
don`t add primary and foreign keys yet). Use descriptive names in constraints.
2. Generate the DDL statements to create the Suppliers table.
3. Add the fields: “status” as numeric, "name" as varchar and “city” as int.
4. Delete the column “status” from the Suppliers table.
5. Rename the column "city" to be called "city_address".
6. Modify the data type of the “city_address” column, so that instead of saving
numbers, it saves a variable string with a maximum length of 50.
7. Shows the structure of the Suppliers table.
8. Generate, with the ALTER statements, all the necessary instructions to link all
the tables as they appear in the diagram.
9. Obtain the relational diagram and compare the results. If necessary, modify your
script to suit the correct one.

Example of how to do some table modification statements with alter table:

If we have the base table:

CREATE TABLE AUTORES_CON_LIBROS


AS SELECT NOMBRE, CODIGO_DIRECCION FROM AUTORES;

ü ALTER TABLE AUTORES_CON_LIBROS ADD (SEXO NUMBER(1) CONSTRAINT


ACL_SEX_NN NOT NULL);

Página 9 de 11
Practice 2
Universidad Autónoma de Zacatecas
Unidad Académica de Ingeniería Eléctrica
Programa Académico de Ingeniería de Software

ü ALTER TABLE AUTORES_CON_LIBROS RENAME COLUMN CODIGO_DIRECCION TO


DIR;
ü ALTER TABLE AUTORES_CON_LIBROS DROP COLUMN DIR;
ü ALTER TABLE AUTORES_CON_LIBROS MODIFY (SEXO CHAR(1));

Another case:
ü ALTER TABLE products DROP CONSTRAINT fk_supplier;
ü ALTER TABLE supplier ADD CONSTRAINT supplier_unique UNIQUE
(supplier_id);
ü ALTER TABLE supplier DISABLE CONSTRAINT supplier_unique;

Activity 7:
Write the section that describes the Work developed in the following activities.

Complete the following ER diagram with the corresponding attributes. The main idea is:
an employee (with their personal data) works on a specific job, which initiated in a
particular date/hour with a description and status. This job is carried out in a branch, in
which the task is located. Thus, a supervisor manages the entire task with a specific date
of assignment.

Using the following E-R diagram.

Generate the corresponding DDL statements. Don`t forget to consider:


• CREATE table statements are the first to be applied. In this case, the basic
constraints must be performed here: not null, default, …
• Use ALTER table statements to add primary and foreign keys.
Página 10 de 11
Practice 2
Universidad Autónoma de Zacatecas
Unidad Académica de Ingeniería Eléctrica
Programa Académico de Ingeniería de Software

• Generate automatically the corresponding relational model using Data Modeler.


• Generate the logical diagram in SQL Data Modeler.
• Compare the results of the ER diagram and the logical diagram.
Activity 8:
Write the section that describes the Work developed in the following activities.

Taking into account your final E-R diagram of activity 2 of practice 1 (Bank scenario).
Generate the corresponding DDL statements. Don`t forget to consider:
• CREATE table statements are the first to be applied. In this case, the basic
constraints must be performed here: not null, default, …
• Use ALTER table statements to add primary and foreign keys.
• Generate automatically the corresponding relational model using Data Modeler
(dragging tables using GUI).
• Compare the results with practice 1.

Activity 9:
Write the Pre-assessment section.

Final activity:
Write the Conclusion section.

Attached file that is required for this task (optional):

e-mail: [email protected]

Página 11 de 11
Practice 2

You might also like