Q # 1
Publisher (PublisherID, PublisherName, Address) Author (AuthorID, AuthorName, Gender, BirthDate, Country) Book (ISBN, AuthorID, PublisherID, PublishDate, BookTitle)
Table Creation
Table Insertion
Q # 2
Constraints
- Account type will either be ‘Current’ or ‘Savings’.
- Customer will have one account only, thus
CustomerID
is unique.
Q # 3
Relation: R
A | B | C | D |
---|---|---|---|
a4 | b1 | c2 | d1 |
a2 | b2 | c1 | d1 |
a2 | b2 | c4 | d2 |
a1 | b4 | c3 | d1 |
Super Keys
- {A, B, C, D}
- {A, C, D}
- {A, B, D}
- {B, C, D}
- {A, B, C}
- {A, D}
- {B, D}
- {A, C}
- {B, C}
- {C, D}
- {C}
Minimal Super Keys / Candidate Keys
Criteria
Super Key whose proper subsets do not belong to another super key.
- {C}
- {B, D}
- {A, D}
Q # 4
1. INSERT Operation
(a)
Fail
Entity Integrity Violation Reason : The Primary Key
ProductID
cannot be specified as null. A Primary Key has to be non null and unique.
(b)
Success
Changes :
- This new record will be inserted into the relation.
(c)
Fail
Entity Integrity Violation Reason : A Primary key by its property cannot be null even if it is composite.
(d)
Fail
Entity Integrity Violation Reason : The Primary key has to be unique but the given value is already present.
(e)
Failure
Referential Integrity Violation Reason : The provided
CustomerID
does not exist in the parent table of this foreign key field.
2. DELETE Operation with CASCADE
(a)
Success
Changes :
- Second record will be deleted from the
Product
relation.- Last record in
Production
relation will also be deleted because of CASCADE option.
(b)
Success
Changes :
- First record will be deleted from the
Customer
relation.- First record in
Order
relation will also be deleted because of CASCADE option.
(c)
Success
Changes :
- Second and third record will be deleted from the
Production
relation.
(d)
Success
Changes :
- All the records in the table will be deleted as they satisfy the requirements.
(e)
Fail
No Changes will be made to the relation as no record will satisfy the criteria.
3. DELETE Operation with SET NULL
(a)
Fail
Referential Integrity Violation Reason : The
ProductID
attribute is a foreign key and a composite primary key inProduction
table. As by entity integrity constraint, a primary key cannot be null thus usingSET NULL
option we will have an error and the operation won’t be executed.
(b)
Success
Changes :
- The first record of
Customer
relation will be deleted- The
CustomerID
inOrder
relation with value1
will be set null.
(c)
Success
Changes :
- The second and third record will be deleted from the relation.
(d)
Success
Changes :
- All the records from the
Order
relation will be discarded.
(e)
Failure
No Changes because the identifier is not present in the relation.
4. Delete Operation - with NO ACTION
(a)
Failure
Referential Integrity Violation Reason : As
ProductID
is being used as Foreign Key inProduction
relation, we will not be allowed to delete the entry in the Parent table until there are no references to that record in any of the child tables.
(b)
Failure
Referential Integrity Violation Reason :
CustomerID
is being used as a Foreign Key inOrder
relation, thus we will not be allowed to delete that.
(c)
Success
Changes :
- The second and third record will be deleted from the
Production
relation.
(d)
Success
Changes :
- All the entries from the
Order
relation will be deleted.
(e)
Success
No Changes will be made because the record is not present in the relation.
5. UPDATE Operation - with CASCADE
(a)
Failure
Referential Integrity Violation
CustomerID
is being used as Foreign Key referencing theCustomer
relation. We will not be allowed to insert or update values that are not present in the referencing attribute.
(b)
Success
Changes :
- The specified
ProductID
inProduct
relation will be updated.- The foreign keys referring to this
ProductID
such as inProduction
andOrder
table will be updated with this new value if they happen refer it.
(c)
Success
Change :
- The
OrderDate
in second and third records will be updated tonull
.
(d)
Failure
SQL Error Reason : The
Production
relation does not have an attributeCustomerID
.
(e)
Failure
Entity Integrity Violation Reason :
BatchNo
is part of a composite primary key in theProduction
relation. Therefore, It’s value cannot be specified asnull
.
6. UPDATE Operation - with SET NULL
(a)
Failure
Referential Integrity Violation The
CustomerID
inCustomer
table does not have value10
in any of the record, so the query will not be executed.
(b)
Failure
Entity Integrity Violation The
ProductID
as being used as Foreign Key inProduction
table, when updated will cause theProduction
table reference to be set asnull
but this will not be allowed as it is a part of a composite primary key.
(c)
Success
Changes : The second and third record of
Order
table will be setOrderDate
asnull
.
(d)
Failure
The given attribute
CustomerID
is not present in theProduction
relation.
(e)
Failure
Entity Integrity Violation As
BatchNo
is part of a composite primary key in theProduction
table, it cannot be updated tonull
.
7. UPDATE Operation - with NO ACTION
(a)
Failure
Referential Integrity Violation The value
10
is not present in the referencing column of Foreign Key attribute.
(b)
Failure
The value will not allowed to be updated because of
NO ACTION
option since this value is being referred by another relation.
(c)
Success
Changes :
- The second and third rows will have
OrderDate
set tonull
.
(d)
Failure
The provided attribute
CustomerID
is not present in theProduction
table.
(e)
Failure
Entity Integrity Violation
BatchNo
is part of a composite primary key, thus it cannot be put asnull
.
Q # 5
SQL Date Formats
Object Name | Format |
---|---|
DATE | YYYY-MM-DD |
YEAR | YYYY / YY |
DATETIME | YYYY-MM-DD HH:MI:SS |
TIMESTAMP | YYYY-MM-DD HH:MI:SS |
For SQL Server
In Addition to above, we also have these:
Object Name | Format |
---|---|
time | HH:MM:SS[.nnnnnn] |
SMALLDATETIME | YYYY-MM-DD HH:MI:SS |
Q # 6
Composite Primary Key
A Composite Primary Key is created by combining two or more attributes in such a way that when taken together, these attributes uniquely identify each record of the relation. They serve as a primary key and may or may not be unique when taken individually.
Example
Consider the following relation :
Name | Eye Color | Age | Cholesterol |
---|---|---|---|
Ahmad | Brown | 19 | 144 |
Saira | Blue | 19 | 184 |
Amjad | Black | 20 | 144 |
The Age
and Cholesterol
attributes serve as a composite primary key.
Composite Foreign Key
A Composite Foreign Key can be created in a situation when we have a composite primary key in the parent table. In this case, we would need to address both of the primary key attributes as foreign key in the child table.
Example
Consider the following relations :
Relation 1
Account Type | Account Num | Account Description |
---|---|---|
Savings | 324325435435 | A Saving Account |
Salary | 243242342342 | A Salary Account |
Relation 2
Customer Name | Account Type | Account Num |
---|---|---|
Ahmad | Salary | 243242342342 |
Account Type
and Account Num
serve as composite primary key in Relation 1
and thus composite foreign key in Relation 2
.