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

ACT 1 Adbms

act 1 in adbms

Uploaded by

cymonneju23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

ACT 1 Adbms

act 1 in adbms

Uploaded by

cymonneju23
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Page |1

INFORMATION TECHNOLOGY AND INFORMATION SYSTEM


DEPARTMENT

ADVANCED DATABASE MANAGEMENT SYSTEM

EXERCISE

1
REVIEW ON CREATING DATABASE OBJECTS

Favila, Cymon Neju B IT 204


NAME SECTION
2/3/2024 2/3/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 (


ID NUMERIC(7),
NAME VARCHAR(25)
);

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

INSERT INTO DEPT (ID, NAME)


SELECT DEPARTMENT_ID,
DEPARTMENT_NAME
FROM DEPARTMENTS;

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

CREATE TABLE EMP2 (


ID NUMERIC(7),
LAST_NAME VARCHAR(25),
FIRST_NAME VARCHAR(25),
DEPT_ID NUMERIC(7)
);

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

ALTER TABLE EMP2


ALTER COLUMN LAST_NAME VARCHAR(50);

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

GJPRosales
Page |7

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.

CREATE TABLE EMPLOYEES2 (


ID INT,
FIRST_NAME VARCHAR(255),
LAST_NAME VARCHAR(255),
SALARY DECIMAL(10, 2),
DEPT_ID INT
);

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

ALTER TABLE EMPLOYEES2


DROP COLUMN FIRST_NAME;

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

SELECT COLUMN_NAME, DATA_TYPE


FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'EMPLOYEES2';

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

ALTER TABLE EMP


ADD CONSTRAINT my_emp_id_pk PRIMARY
KEY (ID);

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.

ALTER TABLE DEPT


ADD CONSTRAINT my_dept_id_pk PRIMARY
KEY (ID);

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.

ALTER TABLE EMP2


ALTER COLUMN DEPT_ID INT;

ALTER TABLE EMP2


ADD CONSTRAINT my_emp_dept_id_fk
FOREIGN KEY (DEPT_ID)
REFERENCES DEPT(ID);

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.

ALTER TABLE EMP2


ADD COMMISSION DECIMAL(2, 2);

ALTER TABLE EMP2


ADD CONSTRAINT
chk_positive_commission CHECK
(COMMISSION > 0);

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

DROP TABLE DEPT;

IV. QUESTION AND ANSWER

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

2. Why do we need to name our constraints?

In databases, naming limitations are essential for effective administration.

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

You might also like