Week-13-14-Data-Definition-Language1 (1)
Week-13-14-Data-Definition-Language1 (1)
EXERCISE
6
USING DDL STATEMENTS TO CREATE AND
MANAGE TABLES
GJPRosales
Page |2
I. OBJECTIVES
Syntax:
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr] [, …]);
Constraints
The MS SQL server uses constraints to prevent invalid data entry into tables.
Syntax:
CREATE TABLE [schema.]table
(column datatype [DEFAULT expr]
GJPRosales
Page |3
[column_constraint],
…
[table_constraint] [, …]);
Syntax:
column [CONSTRAINT constraint_name] constraint_type,
Syntax:
column, …
[CONSTRAINT constraint_name] constraint_type
(column, …),
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];
Syntax:
INSERT INTO table [(column [, column…])]
VALUES (value [, value…]);
Syntax:
INSERT INTO table [(column [, column…])]
subquery;
GJPRosales
Page |4
Syntax:
ALTER TABLE table
ADD (column datatype [DEFAULT expr]
[, column datatype]…);
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]
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.
Task
Write the equivalent SQL statements for the steps that follow.
GJPRosales
Page |5
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.
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.
GJPRosales
Page |7
Step 4: Modify the EMP2 table to allow for longer employee last names.
GJPRosales
Page |8
Step 7: Drop the first name column from the EMPLOYEES2 table.
GJPRosales
Page |9
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
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.
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