Ad22302-Dbms-Unit 2
Ad22302-Dbms-Unit 2
CO2: APPLY SQL QUERIES TO CREATE, MANIPULATE AND QUERY THE DATABASE.
● Relational model concepts
● Integrity constraints
●
TOPICS
SQL Data manipulation
● Views
● SQL programming
RELATIONAL MODEL CONCEPTS
Or
It is called a flat file because each record has a simple linear or flat structure.
EXAMPLE
EXAMPLE
COMPONENTS
Relation : two-dimensional table used to store a collection of data elements.
Attribute/field : column of the relation, representing properties that define the relation.
Attribute domain : set of pre-defined atomic values that an attribute can take i.e., It describes the legal
Relational schema : it is the logical blueprint of the relation i.e., It describes the design and
the structure of the relation. It contains the table name, its attributes, and their types:
For our student relation example, the relational schema will be:
• FOREIGN KEY is a column that creates a relationship between two tables. The
purpose of Foreign keys is to maintain data integrity.
• A FOREIGN KEY is a field (or collection of fields) in one table that refers to the
PRIMARY KEY in another table.
• The "PersonID" column in the "Persons" table is the PRIMARY KEY in the
"Persons" table.
The minimal set of attributes that can uniquely identify a tuple is known
as a candidate key. For Example, STUD_NO in STUDENT relation.
A table can have multiple candidate keys but only a single primary key.
Properties of Candidate key:
• It must contain unique values
• Candidate key may have multiple attributes
• Must not contain null values
• It should contain minimum fields to ensure uniqueness
• Uniquely identify each record in a table
Example: In the given table Stud ID, Roll No, and email are candidate
keys which help us to uniquely identify the student record in the table.
SCHEMA OF UNIVERSITY DATABASE
SCHEMA DIAGRAM OF UNIVERSITY DATABASE
INTEGRITY CONSTRAINTS
create table department (dept name varchar (20), building varchar (15),
budget numeric (12,2), primary key (dept name));
The not null constraint on an attribute specifies that the null value is not allowed for
that attribute.
create table instructor(ID varchar (5), name varchar (20) not null, dept name varchar
(20), salary numeric (8,2), primary key (ID), foreign key (dept name) references
department);
WHAT IS SQL?
● SQL is Structured Query Language, which is a computer language for storing, manipulating and
retrieving data stored in a relational database.
● SQL is the standard language for Relational Database System.
DATABASE LANGUAGES
DATA DEFINITION LANGUAGE
● The language is used to create database, tables, alter them, etc. With this, you can also rename the
database, or drop them. It specifies the database schema.
● The DDL statements include −
● CREATE: Create new database, table, etc.
● ALTER: Alter existing database, table, etc.
● DROP: Drop the database
● RENAME: Set a new name for the table.
● CREATE Command
● The create command defines each column of the table uniquely. Each column has minimum of
three attributes. Each table column definition is separated from the other by a comma. Finally, the
SQL statement is terminated with a semicolon.
● Syntax:
● Create table table_name
● {column_name1 data type1, column_name2 data type2,……….
● column_name n data type n};
● Example:
● Create table student(reg_no int,name char(20),address varchar2(10),dob date);
● DROP Command
● Drop command will destroy the table and all data which will be recorded in it.
● Syntax:
● Drop table table_name;
● Example:
● Drop table student;
RENAME Command
Rename command renames the table name with a new name.
Syntax:
RENAME <OldTableName> TO <NewTableName>
Example:
RENAME <Student> TO <Stu>
ALTER Command: By The use of ALTER TABLE Command we can modify our
exiting table.
Adding New ColumnsThis command is used to add new columns.
Syntax:
ALTER TABLE table_name ADD (NewColumnName DataType......n);
Example:
ALTER TABLE Student ADD (Age number(2), Marks number(3));
DATA MANIPULATION LANGUAGE
● The language used to manipulate the database like inserting data, updating table, retrieving record
from a table, etc. is known as Data Manipulation Language −
● SELECT: Retrieve data from the database
● INSERT: Insert data
● UPDATE: Update data
● DELETE: Delete all records
INSERT command
Insert command is used to insert data into a table.
Syntax:
INSERT into table_name values(data1,data2,..)
Example:
Consider a table Student with following fields.
INSERT into Stud values(101,'Adam’,’Nashik’,15);
Select- SELECT statement retrieves data from table
Syntax
SELECT attribute_list FROM table_list <WHERE condition>;
The attributes are those you want to see in the result, the tables are those required for the
query, the condition is a boolean expression that specifies which tuples are to be retrieved by
the query.
Example: Select * from Stud;
Select * from Stud where rno=101;
Select Name, Address from Stud;
Select Name, Address from Stud where rno=1
Select Name from Stud where address=‘Nashik’;
Update- UPDATE of one or more selected tuples
Syntax
UPDATE tablename SET attrx = new_value, <WHERE condition>;
Example:
Update Stud Set Name= ‘shruti’ where rollno=1 (Only name field of row
having rollno as 1 will be changed as ‘Shruti’)
Update Stud Set Name= ‘Shruti’ (name field of All row will be changed as
‘Shruti’)
Delete –DELETE tuples from relation
Syntax
DELETE FROM tablename <WHERE condition>
It removes zero, one or many tuples; The WHERE clause is optional; if it is left out
all tuples are removed, but the table still exists; it is empty.
Example: DELETE FROM Stud WHERE name= ‘shruti’;
( Only delete rows that satisfy the condition)
DELETE FROM Stud;
(Delete all rows)
UNIVERSITY DATABASE
VIEWS
• Views in SQL are considered as a virtual table. A view also contains rows and
columns.
• To create the view, we can select the fields from one or more tables present in
the database.
• A view can either have specific rows based on certain condition or all the rows
of a table.
• is a single table that is derived from other tables
VIEWS - SYNTAX