0% found this document useful (0 votes)
20 views14 pages

Unit 2 DBMS Part 2

The document discusses integrity constraints in a Database Management System (DBMS), which are rules that maintain data accuracy and consistency. It outlines various types of constraints, including domain, entity integrity, referential integrity, and key constraints, along with their SQL syntax. Additionally, it covers querying relational data, using views, and the operations that can be performed on views in SQL.

Uploaded by

ramspatnala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views14 pages

Unit 2 DBMS Part 2

The document discusses integrity constraints in a Database Management System (DBMS), which are rules that maintain data accuracy and consistency. It outlines various types of constraints, including domain, entity integrity, referential integrity, and key constraints, along with their SQL syntax. Additionally, it covers querying relational data, using views, and the operations that can be performed on views in SQL.

Uploaded by

ramspatnala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Integrity constraint over relations in DBMS

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.

Different types of Integrity Constraints

• Domain Constraint

• Entity Integer Constraint

• Referential Integrity 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.

Entity Integer Constraint


Entity integrity constraints would ensure that null values are not entered into any required
columns. It states that primary key value can't be null. This is because the primary key
value is used to identify individual rows in relation and if the primary key has a null value,
then we can't identify those rows
For example, if you have a column for "roll_number" an entity integrity constraint in DBMS
would ensure that this column cannot contain any null values.
Syntax in sql
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
PRIMARY KEY(column)
);

Referential Integrity Constraint


A referential integrity constraint is a restriction on how foreign keys can be used. A foreign
key is a column in one table that references a primary key in another table.
For example, let's say you have a table of Students and a table of Marks. The
"roll_number" column in the Marks table would be a foreign key that references the
"roll_number" column in the Students table.
Syntax in sql
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
FOREIGN KEY (column) REFERENCES table_name1(column)
);

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:

CREATE TABLE students (


ID int,
LastName varchar(30),
FirstName varchar(30),
Age int,
CONSTRAINT con_age CHECK (Age>=18)
);
DEFAULT Constraint
The DEFAULT constraint is used to set a default value for a column. The default value
will be added to all new records, if no other value is specified.
The DEFAULT constraint can also be used to insert system date, by using functions like
GETDATE().
Syntax in sql
CREATE TABLE table_name (
column1 datatype,
column2 datatype DEFAULT 'value',
column3 datatype DEFAULT GETDATE(),
....
);

Example:

CREATE TABLE students (


ID int,
LastName varchar(30),
FirstName varchar(30),
Age int DEFAULT 18,
joining_date DEFAULT GETDATE()
);

Enforcing Integrity Constraints in DBMS

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.

2. What should we do if a Students row is deleted?


The options are:

☑ 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
);

3. What should we do if the primary key value of a Students row is updated?


The options here are similar to the previous case. Replace ON DELETE with ON
UPDATE
Syntax in sql
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
FOREIGN KEY (column) REFERENCES table_name1(column) ON UPDATE NO
ACTION
);

Example:

CREATE TABLE Enrolled (


Sid int,
Cid varchar(30),
joining_date DEFAULT GETDATE()
FOREIGN KEY (Sid) REFERENCES Students
ON DELETE CASCADE
ON UPDATE NO ACTION
);
Querying Relational Data in DBMS

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:

SELECT * FROM Students WHERE age < 18;

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:

SELECT ID,FirstName FROM Students WHERE age < 18;

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;

Alias Table Syntax in Sql


SELECT column_name(s)
FROM table_name AS alias_name;

Example:

SELECT studentID AS ID,


FROM students AS S;

Aliases can be useful when:

• There are more than one table involved in a query


• Functions are used in the query
• Column names are big or not very readable
• Two or more columns are combined togeth

SELECT data from Multiple Tables


We can also combine information from multiple tables.
Syntax in Sql
SELECT table1.column1, table2.column2
FROM table1, table2
WHERE table1.column1 = table2.column1;

Example:

SELECT S.name, E.cid


FROM Students AS S, Enrolled AS E
WHERE S.sid = E.sid;
Views in DBMS
A view is a table whose rows are not explicitly stored, a view is a virtual table based on
the result-set of an SQL statement. A view can contain all rows of a table or select rows
from a table. A view can be created from one or many tables which depends on the written
SQL query to create a view.
A view is generated to show the information that the end-user requests the data according
to specified needs rather than complete information of the table.

Advantages of View over database tables

• 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:

CREATE VIEW Students_CSE AS


SELECT Roll_no,Name
FROM Students
WHERE Branch = 'CSE';
Updating a View
A view can be updated with the CREATE OR REPLACE VIEW statement.
Syntax in Sql
CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

The following SQL adds the "Mobile" column to the "Students_CSE" view:
Example:

CREATE OR REPLACE VIEW Students_CSE AS


SELECT Roll_no,Name,Mobile
FROM Students
WHERE Branch = 'CSE';

CREATE VIEW defines a view on a set of tables or views or both.


REPLACE VIEW redefines an existing view or, if the specified view does not exist,

Inserting a row in a view


We can insert a row in a View in a same way as we do in a table. We can use the INSERT
INTO statement of SQL to insert a row in a View.
Syntax in Sql
INSERT INTO view_name(column1, column2, ...)
VALUES(value1,value2,.....);

Example:

INSERT INTO Students_CSE(Roll_no,Name,Mobile)


VALUES(521,'ram',9988776655);
Deleting a row in a view
Deleting rows from a view is also as simple as deleting rows from a table. We can use
the DELETE statement of SQL to delete rows from a view.
Syntax in Sql
DELETE FROM view_name
WHERE condition;

Example:

DELETE FROM Students_CSE


WHERE Name="ram";

Querying a View
We can query the view as follows
Syntax in Sql
SELECT * FROM view_name

Example:

SELECT * FROM Students_CSE;

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;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy