ACT 1 Adbms
ACT 1 Adbms
EXERCISE
1
REVIEW ON CREATING DATABASE OBJECTS
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.
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.
Step 4: Modify the EMP2 table to allow for longer employee last names.
GJPRosales
Page |7
Step 7: Drop the first name column from the EMPLOYEES2 table.
GJPRosales
Page |8
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
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
Page |9
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.
Step 13: Drop the EMP2 and DEPT tables so that they cannot be restored
DROP TABLE EMP2;
1. Discuss the difference between table level and column level constraints
Database constraints are the guardians of data integrity. Two types stand out: table-
level and column-level. Table-level constraints oversee the entire table, managing
complex relationships involving multiple columns. Column-level constraints focus on
individual columns, perfect for straightforward rules. Choosing between them depends
on the rule's complexity. Table-level for intricate scenarios, column-level for simplicity.
In essence, both are vital for a reliable and accurate database. They keep data in
check, ensuring a smooth operation.
GJPRosales
P a g e | 10
Rapid Recognition:
Naming limitations guarantees accurate and timely identification in maintenance and
troubleshooting.
Effective Problem-Solving
Clearly labeled limits make troubleshooting easier and give issue resolution professionals
a clear point of reference.
Successful Interaction:
Named constraints make it easier for development teams to communicate effectively and
facilitate database architectural discussions.
Simplified Upkeep:
Named restrictions simplify operations in modifications, adhering to database
management best practices. Naming constraints essentially improve the efficiency and
accuracy of database operations.
3. Discuss the process of creating a new table and creating a targeted table
Making tables in a database is a creative process that needs focus and accuracy.
Creating a new table or modifying an existing one to achieve a particular objective, these
procedures represent a careful balancing act between functionality and data structure.
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