Database 2nd Lab Notes
Database 2nd Lab Notes
LAB 2 WEEK 1
Part 1
Creating a tablespace :
PART 2:
Part II: Data Definition Language
4. Create the basic relationships.
5. Add all constraints
6. List the problems you encountered
7. How did you correct them
2. COURSE Table
3. ENROLLMENT Table
4. TEACHER Table
5. COURSE_ASSIGNMENT Table
6. EXAM Table
7. EXAM_RESULT Table
2. Invalid data type: Verify that the data you are inserting matches the column's data
type. For example, ensure credits is a NUMBER and not a VARCHAR2 .
3. Length issues: Ensure the data being inserted fits within the defined length of the
column. For example, if the course_name column is defined as VARCHAR2(100) ,
make sure you don’t insert a string longer than 100 characte
...
CONSTRAINT pk_constraint_name PRIMARY KEY (column1)
CONSTRAINT fk_constraint_name FORIGEN KEY (nameOfCol)
REFERENCES TABLE_NAME(nameOfCol)
CONSTRAINT check_exam CHECK (ExamType IN ('Midterm' , 'Final' ,
'Quiz')
);
LAB 2 WEEK 2
Part 1 :
to add an attribute to a table ' ALTER TABLE tableName ADD colName datatype'
to add costraints after creating the table : 'ALTER TABLE tableName MODIFY
attribute NOT NULL'
to reduce or enlarge an attribute : 'ALTER TABLE tableName MODIFY attriobute
VARCHAR(50);'
to delete an attribute 'ALTER TABLE TableName DROP COLUMN attributeName; '
to display table information 'DESC tableName'
to rename a column 'ALTER TABLE TableName RENAME COLUMN Address to
StudentAddress;'
to add a check interval constraint : ALTER TABLE TableName ADD CONSTRAINT
check_dates CHECK (StartDate < EndDate);
To reduce:
DESC Student;
UPDATE tableName
SET salary = salary + 600;
WHERE Name = 'sara';
Fill all the tables by the instances
represented above
we use the INSERT statemlent to insert instances into a table .
we used TO_DATE() function to convert a string to the appropiate date format that
must be unified through all other columns : TO_DATE('string', 'format')
Problems
refrential integrity: to insert a record that is linked to another table by id but that
one instance with that id doesn't exist yet
can not insert null value to an attribute that has the constraint 'not_null'
if date data type does not fit in the default form , we simply change only the instance
that caused the problem by chaning its format
UPDATE Teacher
SET Salary = Salary + 5000
WHERE FirstName = 'Sajida' AND LastName = 'Laichi';
UPDATE Course
SET StartDate = StartDate + 5
WHERE EXTRACT(MONTH FROM StartDate) = 1;
EXTRACT function for date datatypes in sql allows extracting day/month feild from
a date datatype.
Potential problems:
Foreign key constraints: If other tables (e.g., enrollments) reference the course,
deleting it could cause foreign key constraint violations.
integrity constraint (DBASCHOOL.FK_EXAM_COURSE) violated - child
record found
other tables use the course_id as foreign key in other tables , so we get an error
to resolve , when I create the foreign key , nzidlo on delete cascade
Definition: ON DELETE CASCADE is a setting for a foreign key in SQL that automatically
deletes related rows in the child table when the corresponding row in the parent table is
deleted. This maintains referential integrity by preventing orphaned records in the child
table.
How to Use: