Unit 2 DBMS Part 2
Unit 2 DBMS Part 2
Integrity constraints are rules that help to maintain the accuracy and consistency of data
in a database. For example, a simple integrity constraint in DBMS might state that all
students must have a valid Roll Number. This would prevent someone from accidentally
entering an invalid roll number into the database.
Integrity constraints can also be used to enforce relationships between tables. For
example, if a student can only have one aadhaar number, then an integrity constraint can
be used to ensure that only one aadhaar number is entered for each student.
• Domain Constraint
• Key Constraints
Domain Constraint
A domain constraint is a restriction on the values that can be stored in a column. Strings,
character, time, integer, currency, date etc. Are examples of the data type of domain
constraints.
example, if you have a column for "age" domain integrity constraints in DBMS would
ensure that only integer values can be entered into that column. This ensures that only
valid data is entered into the database.
Key Constraints
A key constraint is a rule that defines how data in a column(s) can be stored in a table. A
key is composed of one or more columns whose values uniquely identify each row in the
table. There are several different types of key constraints in DBMS, each with its own
specific purpose.
1. Unique Key
A unique key refers to a column or a set of columns that identify every record uniquely in
a table. All the values in this key would have to be unique. values of a unique key won’t
allow duplicate values and it is only capable of having one null value.
Syntax in sql
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
UNIQUE (column)
);
Syntax in sql
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
CONSTRAINT constraint_name UNIQUE(column)
);
2. Primary Key
The primary key refers to a column of a table that helps us identify all the records uniquely
present in that table. Any table can consist of only a single primary key constraint. values
of a primary key won’t allow null value or a duplicate values.
Syntax in sql
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
CONSTRAINT constraint_name PRIMARY KEY(column)
);
3. Foreign Key
We use a foreign key to establish relationships between two available tables. The foreign
key would require every value present in a column/set of columns to match the referential
table’s primary key. A foreign key helps us to maintain data as well as referential integrity.
Syntax in sql
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
CONSTRAINT constraint_name FOREIGN KEY (column)
REFERENCES table_name1(column)
);
4. Composite Key
The composite key refers to a set of multiple attributes that help us uniquely identify every
tuple present in a table. The attributes present in a set may not be unique whenever we
consider them separately. Thus, when we take them all together, it will ensure total
uniqueness.
Syntax in sql
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
CONSTRAINT constraint_name UNIQUE(column,column)
);
5. Super Key
A super key refers to the set of all those keys that help us uniquely identify all the rows
present in a table. It means that all of these columns present in a table that can identify
the columns of that table uniquely act as the super keys.
6. Candidate Key
The candidate keys refer to those attributes that identify rows uniquely in a table. In a
table, we select the primary key from a candidate key. Thus, a candidate key has similar
properties as that of the primary keys that we have explained above. In a table, there can
be multiple candidate keys.
7. Alternate Key
As we have stated above, any table can consist of multiple choices for the primary key.
But, it can only choose one. Thus, all those keys that did not become a primary key are
known as alternate keys.
CHECK Constraint
The CHECK constraint is used to limit the value range that can be placed in a column. If
you define a CHECK constraint on a column it will allow only certain values for this
column. If you define a CHECK constraint on a table it can limit the values in certain
columns based on values in other columns in the row.
Syntax in sql
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
CONSTRAINT constraint_name CHECK (condition)
);
Example:
Example:
Integrity Constraints are specified when a relation is created and enforced when a relation
is modified. The impact of domain, PRIMARY KEY, and UNIQUE constraints is
straightforward: If an insert, delete, or update command causes a violation, it is rejected.
Every potential Integrity violation is generally checked at the end of each SQL statement
execution, although it can be deferred until the end of the transaction executing the
statement.
On the other hand, insertions of Students tuples do not violate referential integrity, and
deletions of Students tuples could cause violations.
SQL provides several alternative ways to handle foreign key violations. We must consider
three basic questions:
1. What should we do if an Enrolled row is inserted, with a student_id column value
that does not appear in any row of the Students table?
In this case, the INSERT command is simply rejected.
☑ Delete all Enrolled rows that refer to the deleted Students row.
Syntax in sql
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
FOREIGN KEY (column) REFERENCES table_name1(column) ON DELETE
CASCADE
);
☑ Disallow the deletion of the Students row if an Enrolled row refers to it.
Syntax in sql
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
FOREIGN KEY (column) REFERENCES table_name1(column) ON DELETE NO
ACTION
);
☑ Set the studid column to the sid of some (existing) 'default' student, for every
Enrolled row that refers to the deleted Students row.
Syntax in sql
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
FOREIGN KEY (column) REFERENCES table_name1(column) ON DELETE SET
DEFAULT
);
Example:
A relational database query is a question about the data, and the answer consists of a
new relation containing the result. For example, we might want to find all students AGE
less than 18 or all students enrolled in perticular course.
The SELECT statement is used to fetch the data from a database table which returns this
data in the form of a result table. These result tables are called result-sets.
Syntax in Sql
SELECT column1, column2, ...
FROM table_name;
If you want to select all the fields available in the table, use the following syntax:
Syntax in Sql
SELECT * FROM table_name;
The symbol ´*´ means that we retain all fields of selected tuples in the result.
We can retrieve rows corresponding to students who are younger than 18 withthe
following SQL query:
Example:
The condition age < 18 in the WHERE clause specifies that we want to select only tuples
in which the age field has a value less than 18.
In addition to selecting a subset of tuples, a query can extract a subset of the fields of
each selected tuple. we can compute the student_id and First_name of students who are
younger than 18 with the following query:
Example:
SQL Aliases
Aliases are the temporary names given to tables or columns. An alias is created with
the AS keyword.
Alias Column Syntax in Sql
SELECT column_name AS alias_name
FROM table_name;
Example:
Example:
• Using Views, we can join multiple tables into a single virtual table.
• Views hide data complexity.
• In the database, views take less space than tables for storing data because the
database contains only the view definition.
• Views indicate the subset of that data, which is contained in the tables of the
database.
Creating Views
Database views are created using the CREATE VIEW statement. Views can be created
from a single table, multiple tables or another view.
To create a view, a user must have the appropriate system privilege according to the
specific implementation.
Syntax in Sql
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Example:
The following SQL adds the "Mobile" column to the "Students_CSE" view:
Example:
Example:
Example:
Querying a View
We can query the view as follows
Syntax in Sql
SELECT * FROM view_name
Example:
Dropping a View
In order to delete a view in a database, we can use the DROP VIEW statement.
Syntax in Sql
DROP FROM view_name
Example:
DROP FROM Students_CSE;