0% found this document useful (0 votes)
4 views

Week-13-14-Data-Definition-Language1 (1)

This document outlines an exercise for students in a Database Management System course, focusing on using DDL statements to create and manage tables. It includes objectives, background information on SQL syntax for creating, altering, and dropping tables, as well as managing constraints. The document also details an experimental procedure with specific tasks to perform, along with a question and answer section discussing constraints and table creation processes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Week-13-14-Data-Definition-Language1 (1)

This document outlines an exercise for students in a Database Management System course, focusing on using DDL statements to create and manage tables. It includes objectives, background information on SQL syntax for creating, altering, and dropping tables, as well as managing constraints. The document also details an experimental procedure with specific tasks to perform, along with a question and answer section discussing constraints and table creation processes.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Page |1

INFORMATION TECHNOLOGY AND INFORMATION SYSTEM


DEPARTMENT

DATABASE MANAGEMENT SYSTEM

EXERCISE

6
USING DDL STATEMENTS TO CREATE AND
MANAGE TABLES

CRUZ, ELISEO D BSIT-201


NAME SECTION
11/25/2024 11/25/2024
DATE PERFORMED DATE FINISHED

GJPRosales
Page |2

I. OBJECTIVES

At the end of this exercise, students must be able to:


a. Create, alter and drop tables
b. Verify that tables exist
c. Manage constraints
d. Add, modify and drop columns

II. BACKGROUND INFORMATION

CREATE TABLE Statement


• This statement is one of the DDL statements that are a subset of the SQL
statements used to create, modify, or remove Oracle database structures.

Syntax:
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr] [, …]);

Constraints
The MS SQL server uses constraints to prevent invalid data entry into tables.

You can use constraints to do the following:


• Enforce rules on the data in a table whenever a row is inserted, updated, or
deleted from that table. The constraint must be satisfied for the operation to
succeed.
• Prevent the deletion of a table if there are dependencies from other tables.

Data Integrity Constraints


Constraint Description
NOT NULL Specifies that the column cannot contain a null value

UNIQUE Specifies a column or combination of columns whose values


must be unique for all rows in the table
PRIMARY KEY Uniquely identifies each row of the table
FOREIGN KEY Establishes and enforces a referential integrity between the
column and a column of the referenced table such that values
in one table match values in another table.
CHECK Specifies a condition that must be true
Defining Constraints

Syntax:
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr]

GJPRosales
Page |3

[column_constraint],

[table_constraint] [, …]);

• Column-level constraint syntax:

Syntax:
column [CONSTRAINT constraint_name] constraint_type,

• Table-level constraint syntax:

Syntax:
column, …
[CONSTRAINT constraint_name] constraint_type
(column, …),

Creating a Table Using a Subquery


• Match the number of specified columns to the number of subquery columns.
• Define columns with column names and default values.

Syntax:
CREATE TABLE table
[(column, column…)]
AS subquery;

Dropping a Table
• Moves a table to the recycle bin
• Removes the table and all its data entirely if the PURGE clause is specified

Syntax:
DROP TABLE table [PURGE];

Inserting Data into the Table


• With this syntax, only one row is inserted at a time

Syntax:
INSERT INTO table [(column [, column…])]
VALUES (value [, value…]);

• Copying rows from another table

Syntax:
INSERT INTO table [(column [, column…])]
subquery;

ALTER TABLE Statement

GJPRosales
Page |4

Syntax:
ALTER TABLE table
ADD (column datatype [DEFAULT expr]
[, column datatype]…);

ALTER TABLE table


MODIFY (column datatype [DEFAULT expr]
[, column datatype]…);

ALTER TABLE table


DROP (column);

Adding a Constraint

Syntax:
ALTER TABLE table
ADD (CONSTRAINT constraint_name] type column_name;

Dropping a Constraint

Syntax:
ALTER TABLE table
DROP PRIMARY KEY | UNIQUE (column) |
CONSTRAINT constraint [CASCADE]

III. EXPERIMENTAL PROCEDURE

Overview

In this exercise, you are to create new tables by using the CREATE TABLE
statement, ALTER TABLE command to modify columns and add constraints, and
confirm that the new table was added to the database. You will also set the status of
a table as READ ONLY and then revert to READ/WRITE.

Note: You will use the DEPARTMENTS and EMPLOYEES table in the HR
schema. Use a qualifier to query data from the table in the HR schema.

Syntax: <schema>.table Example: HR.DEPARTMENTS

DOWNLOAD THE HR SCHEMA FILE

Task

Write the equivalent SQL statements for the steps that follow.

GJPRosales
Page |5

PROVIDE A SCREENSHOT OF THE SCRIPT AND THE OUTPUT PER STEP

Step 1: Create the DEPT table based on the following table instance chart. Enter
the syntax in the SQL Worksheet. Then, execute the statement to create the table.

Column name ID NAME


Data type NUMERIC VARCHAR
Length 7 25

CREATE TABLE
DEPT_table(
ID numeric(7),
NAME varchar(25)
);

Step 2: Populate the DEPT table with data from the DEPARTMENTS table. Include
only columns that you need.

GJPRosales
Page |6

Step 3: Create the EMP2 table based on the following table instance chart. Enter
the syntax in the SQL Worksheet. Then, execute the statement to create the table.

Column name ID LAST_NAME FIRST_NAME DEPT_ID


Data type NUMERIC VARCHAR VARCHAR NUMERIC
Length 7 25 25 7

GJPRosales
Page |7

Step 4: Modify the EMP2 table to allow for longer employee last names.

GJPRosales
Page |8

Step 5: Confirm your modification by checking it in the EMP2 column details.

Step 6: Create the EMPLOYEES2 table based on the structure of the


EMPLOYEES table. Include only the EMPLOYEE_ID, FIRST_NAME,
LAST_NAME, SALARY, and DEPARTMENT_ID columns. Name the columns in
your new table ID, FIRST_NAME, LAST_NAME, SALARY, and DEPT_ID,
respectively.

Step 7: Drop the first name column from the EMPLOYEES2 table.

GJPRosales
Page |9

Step 8: Confirm your modification by checking the description of the table.

Step 9: Add a table-level PRIMARY KEY constraint to the EMP table on the ID
column. The constraint should be named at creation. Name the constraint
my_emp_id_pk

GJPRosales
P a g e | 10

Step 10: Create a PRIMARY KEY constraint to the DEPT table using the ID
column. The constraint should be named at creation. Name the constraint
my_dept_id_pk.

Step 11: Add a foreign key reference on the EMP2 table that ensures that the
employee is not assigned to a nonexistent department. Name the constraint
my_emp_dept_id_fk.

GJPRosales
P a g e | 11

Step 12: Modify the EMP2 table. Add a COMMISSION column of the NUMBER
data type, precision 2, scale 2. Add a constraint to the COMMISSION column that
ensures that a commission value is greater than zero.

GJPRosales
P a g e | 12

Step 13: Drop the EMP2 and DEPT tables so that they cannot be restored

GJPRosales
P a g e | 13

IV. QUESTION AND ANSWER

1. Discuss the difference between table level and column level constraints

From the word itself, column level constraint involve constraint for specific columns, while
Table level may involve single or multiple columns. For example if we want to do a column
Level we could just simply put constraint like PK,Not null or check directly beside the
Column name. As for the table level constraint it is defined separated from the column,
assigning the constraint and Constraint name. For instance Foreign key can span multiple
column when defined at the table level. Although both table and column level constraint
can do single column constraint, table column has much more versatility as It can mani-
pulate or affect multiple columns.

2. Why do we need to name our constraints?

It is much easier for us to know and manage when we have a unique name for our
Constraint, especially when we encounter errors that reflects the issue we may be able
To identify the core column problems. It is also much easier for developers to maintain
Since they have like a notes/comment acting as a guide for theme within the database.
It provides a clear reference on what constraint that column. Lastly the clarity and
Organized database.

3. Discuss the process of creating a new table and creating a targeted table

Creating a new table begins with identifying the data to be stored and defining its
Structure, including the column, data types, and any necessary constraints like
Primary keys or not null conditions. The table is then created using the Create table
Statement in SQL specifying the desired properties. This table is typically designed
for general data storage without a specific focus on performance optimization.
In contrast, creating a targeted table involves a more focused approach, where the table
is designed with a specific purpose or use case in mind, such as supporting analytics
or optimizing for high query performance. Targeted tables often include additional
features like indexing, partitioning, and foreign key relationships, to enhance their-
functionality and efficiency. The goal of a targeted table is to ensure it meets particular
business or technical needs, ensuring optimal performance and data integrity for its
intended purpose.

GJPRosales
P a g e | 14

V. REFERENCES

Hoffer, J.A., Prescott, M.B., McFadden, F.R. (2016). Modern Database Management
12th Edition, Prentice Hall.
Microsoft. (2012). Database Administration Fundamentals . USA: Wiley.

GJPRosales

You might also like