0% found this document useful (0 votes)
11 views9 pages

Unit2 General Notes

Uploaded by

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

Unit2 General Notes

Uploaded by

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

1)EXPLAIN INTEGRITY CONSTRAINTS OVER

RELATION
Integrity constraints are used to ensure accuracy and
consistency of data in a relational database.

 Database integrity refers to the validity and consistency


of stored data. Integrity is usually expressed in terms of
constraints, which are consistency rules that the
database is not permitted to violate. Constraints may
apply to each attribute or they may apply to relationships
between tables.
 Integrity constraints ensure that changes (update
deletion, insertion) made to the database by authorized
users do not result in a loss of data consistency. Thus,
integrity constraints guard against accidental damage to
the database.

TYPES OF INTEGRITY CONSTRAINTS


Various types of integrity constraints are-
1. Domain Integrity
2. Entity Integrity Constraint
3. Referential Integrity Constraint
4. Key Constraints

Domain Integrity-

Domain integrity means the definition of a valid set of values


for an attribute. You define data type, length or size, is null
value allowed , is the value unique or not for an
attribute ,the default value, the range (values in between)
and/or specific values for the attribute.
Entity Integrity Constraint-
This rule states that in any database relation value of
attribute of a primary key can't be null.
EXAMPLE- Consider a relation "STUDENT" Where "Stu_id" is a
primary key and it must not contain any null value whereas
other attributes may contain null value e.g "Branch" in the
following relation contains one null value.
3.Referential Integrity Constraint-

It states that if a foreign key exists in a relation then either the


foreign key value must match a primary key value of some
tuple in its home relation or the foreign key value must be null.
The rules are:

1. You can't delete a record from a primary table if matching


records exist in a related table.
2. You can't change a primary key value in the primary table
if that record has related records.
3. You can't enter a value in the foreign key field of the
related table that doesn't exist in the primary key of the
primary table.
4. However, you can enter a Null value in the foreign key,
specifying that the records are unrelated.

S_id name branch


11255234 Aman

CSE

111 XYZ CSE


112 YXZ IT

EXAMPLE-
Consider 2 relations "stu" and "stu_1" Where "Stu_id " is the
primary key in the "stu" relation and foreign key in the "stu_1"
relation.
Relation "stu"

Examples

Rule 1. You can't delete any of the rows in the ”stu” relation that
are visible since all the ”stu” are in use in the “stu_1” relation.
Rule 2. You can't change any of the ”Stu_id” in the “stu”
relation since all the “Stu_id” are in use in the ”stu_1” relation.
* Rule 3.* The values that you can enter in the” Stu_id” field in
the “stu_1” relation must be in the” Stu_id” field in the “stu”
relation.
Rule 4 You can enter a null value in the "stu_1" relation if the
records are unrelated.
Key Constraints-
A Key Constraint is a statement that a certain minimal subset
of the fields of a relation is a unique identifier for a tuple. The
types of key constraints-

1. Primary key constraints


2. Unique key constraints
3. Foreign Key constraints
4. NOT NULL constraints

1. Primary key constraints

Primary key is the term used to identify one or more columns


in a table that make a row of data unique. Although the
primary key typically consists of one column in a table, more
than one column can comprise the primary key.
For example, either the employee's Social Security number or
an assigned employee identification number is the logical
primary key for an employee table. The objective is for every
record to have a unique primary key or value for the
employee's identification number. Because there is probably
no need to have more than one record for each employee in an
employee table, the employee identification number makes a
logical primary key. The primary key is assigned at table
creation.
The following example identifies the EMP_ID column as the
PRIMARY KEY for the EMPLOYEES table:
3. Foreign Key Constraints
A foreign key is a column in a child table that references a primary key in the
parent table. A foreign key constraint is the main
mechanism used to enforce referential integrity between
tables in a relational database. A column defined as a
foreign key is used to reference a column defined as a
primary key in another table.

Unique Constraints
A unique column constraint in a table is similar to a primary
key in that the value in that column for every row of data in the
table must have a unique value. Although a primary key
constraint is placed on one column, you can place a unique
constraint on another column even though it is not actually for
use as the primary key.
. NOT NULL Constraints

NOT NULL is a constraint that you can place on a table's


column. This constraint disallows the entrance of NULL values
into a column; in other words, data is required in a NOT NULL
column for each row of data in the table. NULL is generally the
default for a column if NOT NULL is not specified, allowing
NULL values in a column.

2.Explain briefly the language supported by database?

Database languages are used to read, update and store data in


a database. There are several such languages that can be
used for this purpose; one of them is SQL (Structured Query
Language).

Data Definition Language (DDL)

DDL is used for specifying the database schema. It is used for


creating tables, schema, indexes, constraints etc. in database.
Lets see the operations that we can perform on database
using DDL:

 To create the database instance – CREATE


 To alter the structure of database – ALTER
 To drop database instances – DROP
 To delete tables in a database instance – TRUNCATE
 To rename database instances – RENAME
 To drop objects from database such as tables – DROP
 To Comment – Comment

Data Manipulation Language (DML)

DML is used for accessing and manipulating data in a


database. The following operations on database comes under
DML:

 To read records from table(s) – SELECT


 To insert record(s) into the table(s) – INSERT
 Update the data in table(s) – UPDATE
 Delete all the records from the table – DELETE

Data Control language (DCL)

DCL is used for granting and revoking user access on a


database –

 To grant access to user – GRANT


 To revoke access from user – REVOKE

Transaction Control Language(TCL)

The changes in the database that we made using DML


commands are either performed or rollbacked using TCL.

 To persist the changes made by DML commands in


database – COMMIT
 To rollback the changes made to the database –
ROLLBACK
3Explain in deatail about views

SQL Views
A VIEW is a virtual table, through which a selective portion of the data from one or
more tables can be seen. Views do not contain data of their own. They are used to
restrict access to the database or to hide data complexity. A view is stored as a
SELECT statement in the database. DML operations on a view like INSERT,
UPDATE, DELETE affects the data in the original table upon which the view is
based.

 view_name is the name of the VIEW.


 The SELECT statement is used to define the columns and rows that you want to
display in the view.
For Example: to create a view on the product table the sql query would be like

SQL Updating a View


CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

SQL Dropping a View


DROP VIEW view_name;

You might also like