Database Constraints Overview

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What happens to the 'Dept' column in the Employee table if a referenced department in the Department table is deleted?

  • The employee record is deleted.
  • The 'Dept' field is set to null. (correct)
  • No action is taken on the employee record.
  • The employee's salary is set to zero.

Which of the following SQL statements correctly alters the Department table to add a new column?

  • alter table Department add (NoOfOffices integer);
  • alter Department add NoOfOffices integer;
  • add column NoOfOffices integer to Department;
  • alter table Department add column NoOfOffices integer; (correct)

What does 'on update cascade' do in the context of foreign key constraints?

  • It resets the child foreign key entries to their default value.
  • It prevents changes to the referenced key in the parent table.
  • It deletes all records in the child table related to the updated parent key.
  • It automatically updates the foreign key in the child table when the referenced key is updated. (correct)

Which SQL command is used to remove a table along with any dependent structures?

<p>drop table TempTable cascade; (C)</p> Signup and view all the answers

In the given Employee table definition, which columns are part of a unique constraint?

<p>Surname and FirstName (B)</p> Signup and view all the answers

What is the purpose of intra-relational constraints?

<p>To ensure conditions are met within a single relation (C)</p> Signup and view all the answers

Which of the following is NOT an example of intra-relational constraint?

<p>Foreign key (B)</p> Signup and view all the answers

What does the 'unique' constraint do in a database table?

<p>Ensures that all entries for the attribute are unique (D)</p> Signup and view all the answers

What do foreign keys facilitate in relational databases?

<p>Referential integrity between multiple relations (B)</p> Signup and view all the answers

What would happen if a foreign key is violated by deleting a referenced row?

<p>The specified reaction policy will be enforced (A)</p> Signup and view all the answers

Which reaction policy would you use to prevent updates to a foreign key table when referenced attributes change?

<p>No action (B)</p> Signup and view all the answers

How do you define an inter-relational constraint in SQL?

<p>By using references after the domain of attributes (C)</p> Signup and view all the answers

Which of the following correctly describes the primary key in a database table?

<p>Uniquely identifies a record within that table (A)</p> Signup and view all the answers

Flashcards are hidden until you start studying

Study Notes

Constraints

  • Constraints are conditions that must be verified by every database instance.
  • There are two constraint types: Intra-relational and Inter-relational constraints.

Intra-relational Constraints

  • These constraints involve a single relation.
  • Not Null: Ensures an attribute cannot be null.
  • Unique: Defines keys, can be applied to single or multiple attributes.
    • Single Attribute: unique is used after the domain definition.
    • Multiple Attributes: unique(Attribute {, Attribute})
  • Primary Key: Defines the primary key which is unique and cannot be null.
    • The syntax is similar to the unique constraint.
  • Check: Described later.

Inter-relational Constraints

  • These constraints involve multiple relations.
  • Check: Described later.
  • References and Foreign Key: Define Referential integrity constraints.
    • Single Attribute: references is applied after the domain definition.
    • Multiple Attributes: foreign key (Attribute {, Attribute }) references ...
  • Reaction policies can be associated with violations of Referential integrity.

Reaction Policies for Referential Integrity Constraints

  • Reactions apply to the table containing the foreign key after changes to the referenced table.
  • Violations can be introduced by:
    • Updating referenced attributes.
    • Deleting rows in the referenced table.
  • Reactions:
    • Cascade: Propagates the change to the related tables.
    • Set Null: Nullifies the referring attributes.
    • Set Default: Assigns the default value to the referring attributes.
    • No Action (Restrict): Forbids changes to the external table.
  • Reactions depend on the event, syntax: on < delete | update > < cascade | set null | set default | no action >

Schema Updates

  • Two SQL statements are used for schema updates:
    • Alter: Used to modify domains, tables, views, and assertions.
    • Drop: Used to remove schemas, domains, tables, views, assertions.
  • Syntax:
    • drop < schema | domain | table | view | assertion > ComponentName [ restrict | cascade ]
  • Example:
    • Alter table Department add column NoOfOffices integer
    • Drop table TempTable cascade

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

SQL-part2.pdf

More Like This

chapter4
10 questions

chapter4

ThankfulOctopus avatar
ThankfulOctopus
Database Integrity Constraints
24 questions

Database Integrity Constraints

UnlimitedWatermelonTourmaline avatar
UnlimitedWatermelonTourmaline
Referential Integrity in Databases
5 questions
Use Quizgecko on...
Browser
Browser