Unit-2 CH I
Unit-2 CH I
INSERT
INTO Students (sid, name, login, age, gpa)
VALUES (53688, 'Smith', 'smith@ee', 18, 3.2);
Or
INSERT INTO table-name VALUES (“val1”, val2, val3,…, “valK”);
•Example Query :
• create table employee(sno int(3), empno varchar(5) primary key, empname
char(10) unique, branch char(5), salary INT(10) not null, designation
varchar(20), place char(10) default ‘Hyderabad’, age varchar(2) check (age
>=18));
ENFORCING INTEGRITY CONSTRAINTS
• ICs 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 IC 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.
• PRIMARY KEY CONSTRAINTS:
• The following insertion violates the primary key constraint because there is already a tuple
with the sid 53688, and it will be rejected by the DBMS:
INSERT
INTO Students (sid, name, login, age, gpa)
VALUES (53688, 'Mike', 'mike@ee', 17,3.4);
• The following insertion violates the constraint that the primary key cannot contain null:
INSERT
INTO Students (sid, name, login, age, gpa)
VALUES (null, 'Mike', 'mike@ee', 17,3.4);
• UPDATE Students S
SET S.sid = 50000
• mysql> create table student5(sid int(10), name varchar(10), age int(3), primary key(sid));
• Query OK, 0 rows affected (0.03 sec)
1 A HYD C1 DBMS 1
2 B Delhi
C2 OS 2
3 A Chennai
Course relation
Student Relation
referencing table
referenced table
• What is Integrity ?
• One value every where throughout the database.
• As the students get added in student table course table will also be
updated according to roll no.
• Course table cannot have a roll no “5” as the roll no “5” is not present in
students table. Here the insertion of “5” in course table will be violated.
• First roll no “5” should be in students table inorder to be in course table.
As Course table is taking a reference from student table’s primary key .
• Course table is called as “referencing table” and student table is called
as “referenced table”
• To create a foreign key :
• Create table course(course_id varchar(10), c_name varchar(20), roll_no
int(10) references student(roll_no));
Constraints for referenced table.
• If anything is inserted in referenced table NO violation.
4 D Goa
1) ON DELETE CASCADE:
• The cascade keyword says that if a student row was deleted all the
course rows that refer to it are also deleted.
• If roll_no 1 is deleted from student relation, automatically row holding
roll_no 1 in course table will also be deleted.
• To create such a foreign key :
• Create table course(course_id varchar(10), c_name varchar(20),
roll_no int(10) references student(roll_no) on delete cascade);
2) ON DELETE SET NULL:
When primary key is deleted each of the referenced foreign key is set to NULL.
• To create such a foreign key :
• Create table course(course_id varchar(10), c_name varchar(20), roll_no
int(10) references student(roll_no) on delete set NULL);
NULL
• But at times a scenario can arise, the referencing table’s foreign key can also
be the primary key of that table. In that case, this solution cannot be
adopted as primary key CANNOT be NULL.
3) ON DELETE NO ACTION:
This states that when the primary key is deleted from the referenced
relation NO ACTION is being performed. When the deletion is done this
gives an error and the deletion is strictly violated here. No change is
done in either of the tables.
4) ON UPDATE CASCADE:
If any updation is to be done in the referenced relation , then that
change should be reflected in referencing relation too. If the roll no in
students table is updated then the course table roll no should also be
updated.
• REFERENCING TABLE :
• INSERT : Inserting any data in referencing table which is not in
referenced table may cause violation
• DELETE: Deleting any data in referencing table will NOT cause
violation
• UPDATE : Updating foreign key data in referencing table which is not
in referenced table may cause violation
Key Constraint on
Manages
CREATE TABLE Manages (ssn CHAR (11) ,
did INTEGER,
since DATE,
PRIMARY KEY (did),
FOREIGN KEY (ssn) REFERENCES Employees,
FOREIGN KEY (did) REFERENCES Departments)
second approach to translating a relationship set with key constraints is
often superior because it avoids creating a distinct table for the relationship
set. The idea is to include the information about the relationship set in the
table corresponding to the entity set with the key, taking advantage of the
key constraint. In the Manages example, because a department has at most
one manager, we can add the key fields of the Employees tuple denoting the
manager and the since attribute to the Departments tuple.
monitors
• The Employees, Projects, and Departments entity sets and the
Sponsors relationship set are mapped as described in previous
sections. For the Monitors relationship set, we create a relation with
the following attributes: the key attributes of Employees (ssn), the key
attributes of Sponsors (did, pid), and the descriptive attributes of
Monitors until.
ER to Relational: Additional Examples
CREATE TABLE Policies
( policyid INTEGER,
cost REAL,
ssn CHAR (11) NOT NULL,
PRIMARY KEY (policyid), CREATE TABLE Dependents (pname
FOREIGN KEY (ssn) CHAR(20) ,
REFERENCES Employees ssn CHAR (11) ,
ON DELETE CASCADE ) age INTEGER,
policyid INTEGER NOT NULL,
PRIMARY KEY (pname, policyid, ssn),
FOREIGN KEY (policyid, ssn)
REFERENCES Policies
ON DELETE CASCADE )
• We can use the key constraints to combine Purchaser information
with Policies and Beneficiary information with Dependents, and
translate it into the relational model.
• Notice how the deletion of an employee leads to the deletion of all
policies owned by the employee and all dependents who are
beneficiaries of those policies.
INTRODUCTION TO VIEWS IN SQL:
• Views are derived or virtual tables, they derive their data from the
original relation.
• A view also contains rows and columns and to create the view, we can
select the fields from one or more tables present in the database.
• A view rows are not explicitly stored in the database but are computed
as needed from a view definition.
• A view can either have specific rows based on certain condition or all
the rows of a table.
• Views allows the user to see only the part of data but not all data.
• Views are dynamic in nature i.e. the changes made to the views are
reflected back to the original table and vice versa.
Creation of Views
Syntax:
CREATE VIEW View-name (field1,field2….fieldn)
AS SELECT (attribute1,attribute2….attributen)
FROM table1,table2…tablen
WHERE <condition>;
Student
Marks
Creating view from single table
create view details as
select name, address from student
where S_ID<5;
-Just like table query we can query the view to display the data stored in the
views.
Select * from details;
CREATE
VIEW
• CREATE VIEW StudentNames AS
SELECT S_ID, NAME FROM Student
ORDER BY NAME;
mysql>
DESTROYING/ALTERING TABLES AND VIEWS
• If we decide that we no longer need a base table and want to
destroy it (i.e., delete all the rows and remove the table
definition information), we can use the DROP TABLE
command.
• For example, DROP TABLE Faculty RESTRICT destroys the
Faculty table unless some view or integrity constraint refers to
Faculty; if so, the command fails.
• If the keyword RESTRICT is replaced by CASCADE,
Faculty is dropped and any referencing views or integrity
constraints
• ALTER TABLE modifies the structure of an existing table. To
add a column called maiden-name to Students, for example, we
would use the following command.