Practice for DDL and DML Commands
Objective
This assignment will help you practice and master:
DDL (Data Definition Language)
- CREATE
- ALTER
- DROP
- TRUNCATE
- RENAME
DML (Data Manipulation Language)
- INSERT
- UPDATE
- DELETE
- MERGE
You will also practice constraints, keys, and real-world data operations.
Part 1: Database Design Scenario
Create a College Management System Database with the following tables:
- Students
- Courses
- Faculty
- Enrollments
Part 2: DDL Practice
Task 1: Create Tables
Create the following tables with appropriate data types.
STUDENTS
| Column | Type | Constraint |
|---|---|---|
| student_id | NUMBER | Primary Key |
| first_name | VARCHAR2(30) | NOT NULL |
| last_name | VARCHAR2(30) | NOT NULL |
| VARCHAR2(50) | UNIQUE | |
| phone | VARCHAR2(15) | |
| admission_date | DATE |
COURSES
| Column | Type | Constraint |
|---|---|---|
| course_id | NUMBER | Primary Key |
| course_name | VARCHAR2(50) | NOT NULL |
| credits | NUMBER | |
| department | VARCHAR2(50) |
FACULTY
| Column | Type | Constraint |
|---|---|---|
| faculty_id | NUMBER | Primary Key |
| faculty_name | VARCHAR2(50) | |
| department | VARCHAR2(50) | |
| salary | NUMBER |
ENROLLMENTS
| Column | Type | Constraint |
|---|---|---|
| enrollment_id | NUMBER | Primary Key |
| student_id | NUMBER | Foreign Key |
| course_id | NUMBER | Foreign Key |
| enrollment_date | DATE |
Task 2: Add Constraints (ALTER)
Perform the following:
- Add FOREIGN KEY from
student_idin enrollments referencing students. - Add FOREIGN KEY from
course_idreferencing courses. - Add CHECK constraint for credits (between 1 and 5).
- Add DEFAULT value for admission_date.
Task 3: Modify Table Structure
Perform the following operations:
- Add a column
genderto students. - Add column
hire_dateto faculty. - Modify
salarycolumn to allow larger values. - Rename column
phonetocontact_number. - Drop column
gender.
Task 4: Table Management
Practice the following commands:
- Rename table
facultytoprofessors. - Truncate table
enrollments. - Drop table
professors.
Part 3: DML Practice
Task 5: Insert Data
Insert at least 5 records in each table.
Example:
Students
Courses
Faculty
Enrollments
Use both:
- Single row insert
- Multiple row insert
- Insert using subquery
Task 6: Update Records
Perform the following updates:
- Change student phone number.
- Increase faculty salary by 10%.
- Change course credits for a specific course.
- Update department name.
Task 7: Delete Records
Practice different delete scenarios.
- Delete a specific student.
- Delete courses with less than 2 credits.
- Delete enrollments of a specific course.
- Delete all records from a table without dropping the structure.
Task 8: MERGE Statement
Create a new table:
new_students
Insert some records in it.
Now use MERGE to:
- Update existing students
- Insert new students
Part 4: Advanced DML Practice
Task 9: Insert Using Subquery
Insert records into enrollments using a subquery from students and courses.
Task 10: Update Using Subquery
Update student admission date using a subquery condition.
Task 11: Delete Using Subquery
Delete enrollments where the course belongs to a specific department.
Part 5: Practice Queries (Real World Tasks)
Perform the following:
- Add a NOT NULL constraint to course_name.
- Add a UNIQUE constraint to faculty email.
- Create a table backup_students using:
CREATE TABLE backup_students AS SELECT * FROM students;
- Insert records into backup_students from students.
- Drop backup_students table.
Part 6: Challenge Tasks (For Mastery)
Try the following advanced exercises.
- Create a table using CREATE TABLE AS SELECT.
- Add multiple constraints in a single ALTER command.
- Write a MERGE statement that:
- updates matching rows
- inserts non-matching rows
- Write a script to:
- create tables
- insert data
- update records
- delete records
Expected Learning Outcomes
After completing this assignment, you should be able to:
✔ Create and modify tables
✔ Apply constraints correctly
✔ Insert, update, and delete data
✔ Use MERGE operations
✔ Modify table structure
✔ Manage tables in Oracle SQL
