Relational Model in DBMS Unit2
Relational Model in DBMS Unit2
4 SURESH DELHI 18
Important Terminologies
Attribute: Attributes are the properties that define an entity. e.g.; ROLL_NO, NAME,
ADDRESS
Relation Schema: A relation schema defines the structure of the relation and represents
the name of the relation with its attributes. e.g.; STUDENT (ROLL_NO, NAME,
ADDRESS, PHONE, and AGE) is the relation schema for STUDENT. If a schema has
more than 1 relation, it is called Relational Schema.
Tuple: Each row in the relation is known as a tuple. The above relation contains 4 tuples,
one of which is shown as:
1
Relation Instance: The set of tuples of a relation at a particular instance of time is called
a relation instance. Table 1 shows the relation instance of STUDENT at a particular time.
It can change whenever there is an insertion, deletion, or update in the database.
Degree: The number of attributes in the relation is known as the degree of the relation.
The STUDENT relation defined above has degree 5.
Cardinality: The number of tuples in a relation is known as cardinality.
The STUDENT relation defined above has cardinality 4.
Column: The column represents the set of values for a particular attribute. The
column ROLL_NO is extracted from the relation STUDENT.
ROLL_NO
NULL Values: The value which is not known or unavailable is called a NULL value. It is
represented by blank space. e.g.; PHONE of STUDENT having ROLL_NO 4 is NULL.
Tables: relations are saved in the table format. A table has two properties rows and
columns
Attribute: columns represent as attributes
Tuple: A Row represent as Tuple
Relation Schema: A relation schema represents the name of the relation with its
attributes.
Degree: The total number of attributes which in the relation is called the degree of the
relation.
Cardinality: Total number of rows present in the Table.
Column: The column represents the set of values for a specific attribute.
Relation instance: The set of tuples of a relation at a particular instance of time is
called as relation instance.
Conceptual Simplicity: The relational model allows the designer to simply focus on
logical design and not on physical design. Hence relational models are conceptually
simple to understand.
Query Capability: Using simple query language (such as SQL) user can get
information from the database or designer can manipulate the database structure.
Relational model requires powerful hardware and large data storage devices.
May lead to slower processing time.
Poorly designed systems lead to poor implementation of database systems.
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
3
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.
These are attribute-level constraints. An attribute can only take values that lie inside the
domain range. e.g.; If a constraint AGE>0 is applied to STUDENT relation, inserting a
negative value of AGE will result in failure.
Example-
Consider the following Student table-
S001 Akshay 20
S002 Abhishek 21
S003 Shashank 20
S004 Rahul A
Here, value ‘A’ is not allowed since only integer values can be taken by the age attribute.
For example, if you have a column for ”STU_ID" an entity integrity constraint in DBMS
would ensure that this column cannot contain any null values.
4
Every relation in the database should have at least one set of attributes that defines a
tuple uniquely. Those set of attributes is called keys. e.g.; STU_ID in STUDENT is key. No
two students can have the same roll number. So a key has two properties:
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.
When one attribute of a relation can only take values from another attribute of the
same relation or any other relation, it is called referential integrity. Let us suppose we have 2
relations
Table Student
4 SURESH DELHI 18 IT
Table Branch
5
BRANCH_CODE BRANCH_NAME
CS COMPUTER SCIENCE
IT INFORMATION TECHNOLOGY
CV CIVIL ENGINEERING
BRANCH_CODE of STUDENT can only take the values which are present in
BRANCH_CODE of BRANCH which is called referential integrity constraint. The relation
which is referencing another relation is called REFERENCING RELATION (STUDENT in this
case) and the relation to which other relations refer is called REFERENCED RELATION
(BRANCH in this case).
Here,
The relation ‘Student’ does not satisfy the referential integrity constraint.
This is because in relation ‘BRANCH_CODE’, no value of primary key specifies
department AIML.
Thus, referential integrity constraint is violated.
Key Constraints
6
From above employee table john monthly salary is hike. But from above table total 3 names
started with the name john. now data base confuse which record is update among three
starting with the name john. That why we need one unique id (primary key).
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.
7
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.
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 below. In a table,
there can be multiple candidate keys.
Primary Key
8
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 Mysql
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
CONSTRAINT constraint_name PRIMARY KEY(column)
);
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.
9
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 Mysql
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
UNIQUE (column)
);
10
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 Mysql
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
CONSTRAINT constraint_name UNIQUE(column,column)
);
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.
11
Syntax in Mysql
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
CONSTRAINT constraint_name FOREIGN KEY (column)
REFERENCES table_name1(column)
);
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 Mysql
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
....
CONSTRAINT constraint_name CHECK (condition)
);
Example:
12
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 Mysql
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()
);
SELECT Statement
The SELECT statement is the most commonly used command in Structured Query
Language. It is used to access the records from one or more database tables and views.
It also retrieves the selected data that follow the conditions we want.
By using this command, we can also access the particular record from the particular
column of the table. The table which stores the record returned by the SELECT
statement is called a result-set table.
14
If you want to access all rows from all fields of the table, use the following SQL SELECT
syntax with * asterisk sign:
Example 1:
Firstly, we have to create the new table and then insert some dummy records into it.
1. INSERT INTO Student VALUES (201, Akash, Delhi, 18, 89, A2),
2. (202, Bhavesh, Kanpur, 19, 93, A1),
3. (203, Yash, Delhi, 20, 89, A2),
4. (204, Bhavna, Delhi, 19, 78, B1),
5. (05, Yatin, Lucknow, 20, 75, B1),
6. (206, Ishika, Ghaziabad, 19, 51, C1),
7. (207, Vivek, Goa, 20, 62, B2);
The following SQL query displays all the values of each column from the above
Student_records table:
15
The output of the above query is:
Example 2:
The following query displays the values of particular column from the
above Student_Record table:
201 18 89 A2
202 19 93 A1
203 20 89 A2
204 19 78 B1
205 20 75 B1
206 19 91 C1
207 20 80 B2
16
The WHERE clause is used with SELECT statement to return only those rows from the
table, which satisfy the specified condition in the query.
In SQL, the WHERE clause is not only used with SELECT, but it is also used with other
SQL statements such as UPDATE, ALTER, and DELETE statements.
In the syntax, we specify the condition in the WHERE clause using SQL logical or
comparison operators.
Firstly, we have to create the new table and then insert some dummy records into it.
The following INSERT query inserts the record of employees into the Employee_Details
table:
The following SELECT query shows the data of the Employee_Details table:
17
Employee_Id Emp_Name Emp_City Emp_Salary Emp_Panelty
The following query shows the record of those employees from the above table whose
Emp_Panelty is 500:
18
3. Car_Number INT PRIMARY KEY,
4. Car_Name VARCHAR (50),
5. Car_Price INT NOT NULL,
6. Car_AmountINT NOT NULL
7. ) ;
The following INSERT query inserts the record of cars into the Cars_Details table:
The following SELECT with GROUP BY query lists the number of cars of the same price:
Output:
2 1000000
2 900000
19
SQL SELECT Statement with HAVING clause
The HAVING clause in the SELECT statement creates a selection in those groups which
are defined by the GROUP BY clause.
Let's create the Employee_Having table in SQL using the below CREATE command:
The following INSERT query inserts the record of employees into the Employee_Having
table:
The following SELECT query shows the values of Employee_Having table in the output:
20
201 Jone 20000 Goa
The following query shows the total salary of those employees having more than 5000
from the above Employee_Having table:
This HAVING query with SELECT statement shows the following table:
Output:
90000 Delhi
80000 Jaipur
The ORDER BY clause arranges the values in both ascending and descending order. Few
database systems arrange the values of column in ascending order by default.
The following INSERT query inserts the record of employees into the Employee_Having
table:
The following SELECT query shows the values of the table in the output:
The following query sorts the salary of employees in descending order from the above
Employee_Order table:
Output:
22
Emp_Id Emp_Name Emp_Salary Emp_City
Logical Database
23
Features of Logical Database:
In this section, let us look at some features of a logical database:
We can select only that type of Data that we need.
Data Authentication is done in order to maintain security.
Logical Database uses hierarchical Structure due to this data integrity is
maintained.
Goal Of Logical Database:
The goal of Logical Database is to create well-structured tables that reflect the
need of the user. The tables of the Logical database store data in a non-
redundant manner and foreign keys will be used in tables so that relationships
among tables and entities will be supported.
Tasks Of Logical Database:
Below is some important task of Logical Database:
With the help of the Logical database, we will read the same data from
multiple programs.
A logical database defines the same user interface for multiple programs.
Logical Database ensures the Authorization checks for the centralized
sensitive database.
With the help of a Logical Database, Performance is improved. Like in
Logical Database we will use joins instead of multiple SELECT statements,
which will improve response time and this will increase the Performance of
Logical Database.
24
Data View Of Logical Database:
Logical Database provides a particular view of Logical Database tables. A
logical database is appropriately used when the structure of the Database is
Large. It is convenient to use flow i.e
SELECT
READ
PROCESS
DISPLAY
In order to work with databases efficiently. The data of the Logical Database is
hierarchical in nature. The tables are linked to each other in a Foreign Key
relationship.
Diagrammatically, the Data View of Logical Database is shown as: Tables must
have Foreign Key Relationship.
A logical Database consists of logically related tables that are arranged in a
hierarchical manner used for reading or retrieving Data.
Logical Database consist of three main elements:
Structure of Database
Selections of Data from Database
Database Program
If we want to improve the access time on data, then we use VIEWS in
Logical Database.
Example:
Suppose in a University or College, a HOD wants to get information about a
specific student. So for that, he firstly retrieves the data about its batch and
Branch from a large amount of Data, and he will easily get information about
the required Student but didn’t alter the information about it.
Advantages Of Logical Database:
Let us look at some advantages of the logical database:
In a Logical database, we can select meaningful data from a large amount
of data.
Logical Database consists of Central Authorization which checks for
Database Accesses is Authenticated or not.
In this Coding, the part is less required to retrieve data from the database as
compared to Other Databases.
Access performance of reading data from the hierarchical structure of the
Database is good.
Easy to understand user interfaces.
Logical Database firstly check functions which further check that user input
is complete, correct, and plausible.
25
Disadvantages Of Logical Database:
This section shows the disadvantages of the logical database:
Logical Database takes more time when the required data is at the last
because if that table which is required at the lowest level then firstly all
upper-level tables should be read which takes more time and this slows
down the performance.
In Logical Database ENDGET command doesn’t exist due to this the code
block associated with an event ends with the next event statement.
Views in SQL
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.
In the database, views take less space than tables for storing data
because the database contains only the view definition.
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 Mysql
26
CREATE VIEW view_name AS
FROM table_name
WHERE condition;
Example:
Updating a View
Syntax in Mysql
FROM table_name
WHERE condition;
The following SQL adds the "Mobile" column to the "Students_CSE" view:
Example:
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 Mysql
VALUES(value1,value2,.....);
Example:
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 Mysql
28
DELETE FROM view_name
WHERE condition;
Example:
Querying a View
Syntax in Mysql
Example:
Dropping a View
In order to delete a view in a database, we can use the DROP VIEW statement.
Syntax in Mysql
29
Example:
o Views in SQL are considered as a virtual table. A view also contains rows and
columns.
o To create the view, we can select the fields from one or more tables present in
the database.
o A view can either have specific rows based on certain condition or all the rows of
a table.
Student_Detail
1 Stephan Delhi
2 Kathrin Noida
3 David Ghaziabad
4 Alina Gurugram
Student_Marks
1 Stephan 97 19
2 Kathrin 86 21
3 David 74 18
4 Alina 90 20
5 John 96 18
1. Creating view
30
A view can be created using the CREATE VIEW statement. We can create a view from a
single table or multiple tables.
Syntax:
Query:
Just like table query, we can query the view to view the data.
Output:
NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad
In the given example, a view is created named MarksView from two tables
Student_Detail and Student_Marks.
31
Query:
Stephan Delhi 97
Kathrin Noida 86
David Ghaziabad 74
Alina Gurugram 90
4. Deleting View
A view can be deleted using the Drop View statement.
Syntax
Example:
32
DBMS Relational Algebra
Types of operations in relational algebra
We have divided these operations in two categories:
1. Basic Operations
2. Derived Operations
Basic/Fundamental Operations:
1. Select (σ)
2. Project (∏)
3. Union (∪)
4. Set Difference (-)
5. Cartesian product (X)
6. Rename (ρ)
Derived Operations:
1. Natural Join (⋈)
2. Left, Right, Full outer join (⟕, ⟖, ⟗)
3. Intersection (∩)
4. Division (÷)
Lets discuss these operations one by one with the help of examples.
If you understand little bit of SQL then you can think of it as a where clause in SQL,
which is used for the same purpose.
σ Condition/Predicate(Relation/Table name)
Customer_IdCustomer_NameCustomer_City
----------- ------------- -------------
33
C10100 SteveAgra
C10111 RaghuAgra
C10115 ChaitanyaNoida
C10117 AjeetDelhi
C10118 CarlDelhi
Query:
σ Customer_City="Agra" (CUSTOMER)
Output:
Customer_IdCustomer_NameCustomer_City
----------- ------------- -------------
C10100 SteveAgra
C10111 RaghuAgra
Table: CUSTOMER
Customer_IdCustomer_NameCustomer_City
----------- ------------- -------------
C10100 SteveAgra
C10111 RaghuAgra
C10115 ChaitanyaNoida
C10117 AjeetDelhi
C10118 CarlDelhi
Query:
Customer_NameCustomer_City
------------- -------------
SteveAgra
34
RaghuAgra
ChaitanyaNoida
AjeetDelhi
CarlDelhi
Lets discuss union operator a bit more. Lets say we have two relations R1 and R2 both
have same columns and we want to select all the tuples(rows) from these relations then
we can apply the union operator on these relations.
Note: The rows (tuples) that are present in both the tables will only appear once in the
union set. In short you can say that there are no duplicates present after the union
operation.
table_name1 ∪ table_name2
Course_IdStudent_NameStudent_Id
--------- ------------ ----------
C101 Aditya S901
C104 Aditya S901
C106 Steve S911
C109 Paul S921
C115 Lucy S931
Table 2: STUDENT
Student_IdStudent_NameStudent_Age
------------ ---------- -----------
S901 Aditya19
S911 Steve18
S921 Paul19
S931 Lucy17
S941 Carl16
S951 Rick18
Query:
35
Student_Name
------------
Aditya
Carl
Paul
Lucy
Rick
Steve
Note: As you can see there are no duplicate names present in the output even though we
had few common names in both the tables, also in the COURSE table we had the
duplicate name itself.
Lets say we have two relations R1 and R2 both have same columns and we want to
select all those tuples(rows) that are present in both the relations, then in that case we
can apply intersection operation on these two relations R1 ∩ R2.
Note: Only those rows that are present in both the tables will appear in the result set.
table_name1 ∩ table_name2
Intersection Operator (∩) Example
Lets take the same example that we have taken above.
Table 1: COURSE
Course_IdStudent_NameStudent_Id
--------- ------------ ----------
C101 Aditya S901
C104 Aditya S901
C106 Steve S911
C109 Paul S921
C115 Lucy S931
Table 2: STUDENT
Student_IdStudent_NameStudent_Age
------------ ---------- -----------
S901 Aditya19
S911 Steve18
S921 Paul19
S931 Lucy17
S941 Carl16
36
S951 Rick18
Query:
Student_Name
------------
Aditya
Steve
Paul
Lucy
table_name1 - table_name2
Set Difference (-) Example
Lets take the same tables COURSE and STUDENT that we have seen above.
Query:
Lets write a query to select those student names that are present in STUDENT table but
not present in COURSE table.
Student_Name
------------
Carl
Rick
Col_ACol_B
----- ------
AA 100
BB 200
CC 300
Table 2: S
Col_XCol_Y
----- -----
XX 99
YY 11
ZZ 101
Query:
Lets find the cartesian product of table R and S.
R X S
Output:
Col_ACol_BCol_XCol_Y
----- ------ ------ ------
AA 100 XX 99
AA 100 YY 11
AA 100 ZZ 101
BB 200 XX 99
BB 200 YY 11
BB 200 ZZ 101
CC 300 XX 99
CC 300 YY 11
CC 300 ZZ 101
Note: The number of rows in the output will always be the cross product of number of
rows in each table. In our example table 1 has 3 rows and table 2 has 3 rows so the
output has 3×3 = 9 rows.
Rename (ρ)
Rename (ρ) operation can be used to rename a relation or an attribute of a relation.
Rename (ρ) Syntax:
ρ(new_relation_name, old_relation_name)
38
Lets say we have a table customer, we are fetching customer names and we are renaming
the resulted relation to CUST_NAMES.
Table: CUSTOMER
Customer_IdCustomer_NameCustomer_City
----------- ------------- -------------
C10100 SteveAgra
C10111 RaghuAgra
C10115 ChaitanyaNoida
C10117 AjeetDelhi
C10118 CarlDelhi
Query:
ρ(CUST_NAMES, ∏(Customer_Name)(CUSTOMER))
Output:
CUST_NAMES
----------
Steve
Raghu
Chaitanya
Ajeet
Carl
39
1. Tuple Relational Calculus (TRC)
Tuple relational calculus is used for selecting those tuples that satisfy the given
condition.
Table: Student
First_NameLast_NameAge
-----------------------
AjeetSingh30
ChaitanyaSingh31
RajeevBhatia27
CarlPratap28
Lets write relational calculus queries.
Query to display the last name of those students where age is greater than 30
Last_Name
---------
Singh
Query to display all the details of students where Last name is ‘Singh’
First_NameLast_NameAge
-----------------------
AjeetSingh30
ChaitanyaSingh31
First_NameLast_NameAge
-----------------------
AjeetSingh30
ChaitanyaSingh31
40
RajeevBhatia27
CarlPratap28
Query to find the first name and age of students where student age is greater than 27
{<First_Name,Age>|∈Student∧Age>27}
Note:
The symbols used for logical operators are: ∧ for AND, ∨ for OR and ┓ for NOT.
Output:
First_NameAge
--------------
Ajeet30
Chaitanya31
Carl28
The DDL Commands in Structured Query Language are used to create and modify the
schema of the database and its objects. The syntax of DDL commands is predefined for
describing the data. The commands of Data Definition Language deal with how the
data should exist in the database.
1. CREATE Command
2. DROP Command
3. ALTER Command
4. TRUNCATE Command
5. RENAME Command
CREATE Command
CREATE is a DDL command used to create databases, tables, triggers and other
database objects.
Suppose, you want to create a Books database in the SQL database. To do this, you
have to write the following DDL Command:
Example 2: This example describes how to create a new table using the CREATE
DDL command.
Suppose, you want to create a Student table with five columns in the SQL database. To
do this, you have to write the following DDL command:
Example 3: This example describes how to create a new index using the CREATE
DDL command.
Suppose, you want to create an index on the combination of the City and State field of
the Student table. For this, we have to use the following DDL command:
Example 4: This example describes how to create a trigger in the SQL database
using the DDL CREATE command.
DROP Command
DROP is a DDL command used to delete/remove the database objects from the SQL
database. We can easily remove the entire table, view, or index from the database using
this DDL command.
43
Suppose, you want to delete the Books database from the SQL database. To do this,
you have to write the following DDL command:
Example 2: This example describes how to remove the existing table from the SQL
database.
Suppose, you want to delete the Student table from the SQL database. To do this, you
have to write the following DDL command:
Example 3: This example describes how to remove the existing index from the
SQL database.
Suppose, you want to delete the index_city from the SQL database. To do this, you have
to write the following DDL command:
ALTER Command
ALTER is a DDL command which changes or modifies the existing structure of the
database, and it also changes the schema of database objects.
We can also add and drop constraints of the table using the ALTER command.
44
Suppose, you want to add the 'Father's_Name' column in the existing Student table. To
do this, you have to write the following DDL command:
Example 2: This example describes how to remove the existing column from the
table.
Suppose, you want to remove the Age and Marks column from the existing Student
table. To do this, you have to write the following DDL command:
Example 3: This example describes how to modify the existing column of the
existing table.
Suppose, you want to change the character size of the Last_Namefield of the Student
table. To do this, you have to write the following DDL command:
TRUNCATE Command
TRUNCATE is another DDL command which deletes or removes all the records from the
table.
This command also removes the space allocated for storing the table records.
Example
45
Suppose, you want to delete the record of the Student table. To do this, you have to
write the following TRUNCATE DDL command:
The above query successfully removed all the records from the student table. Let's
verify it by using the following SELECT statement:
RENAME Command
RENAME is a DDL command which is used to change the name of the database table.
Example
This query changes the name of the table from Student to Student_Details.
The DML commands in Structured Query Language change the data present in the SQL
database. We can easily access, store, modify, update and delete the existing records
from the database using DML commands.
1. SELECT Command
2. INSERT Command
46
3. UPDATE Command
4. DELETE Command
If we want to retrieve the data from all the columns of the table, we have to use the
following SELECT command:
This SQL statement displays the following values of the student table:
BCA1001 Abhay 85
BCA1002 Anuj 75
BCA1003 Bheem 60
BCA1004 Ram 79
BCA1005 Sumit 80
Example 2: This example shows all the values of a specific column from the table.
47
1. SELECT Emp_Id, Emp_Salary FROM Employee;
This SELECT statement displays all the values of Emp_Salary and Emp_Id column
of Employee table:
Emp_Id Emp_Salary
201 25000
202 45000
203 30000
204 29000
205 40000
Example 3: This example describes how to use the WHERE clause with the SELECT
DML command.
BCA1001 Abhay 80
BCA1002 Ankit 75
BCA1003 Bheem 80
BCA1004 Ram 79
BCA1005 Sumit 80
If you want to access all the records of those students whose marks is 80 from the
above table, then you have to write the following DML command in SQL:
48
BCA1001 Abhay 80
BCA1003 Bheem 80
BCA1005 Sumit 80
Let's take the following student table, which consists of only 2 records of the student.
101 Ramesh 92 20
201 Jatin 83 19
Suppose, you want to insert a new record into the student table. For this, you have to
write the following DML INSERT command:
49
1. UPDATE Table_name SET [column_name1= value_1, ….., column_nameN = value
_N] WHERE CONDITION;
Here, 'UPDATE', 'SET', and 'WHERE' are the SQL keywords, and 'Table_name' is the name
of the table whose values you want to update.
P101 Chips 20 20
P102 Chocolates 60 40
P103 Maggi 75 5
P201 Biscuits 80 20
P203 Namkeen 40 50
Suppose, you want to update the Product_Price of the product whose Product_Id is
P102. To do this, you have to write the following DML UPDATE command:
Example 2: This example describes how to update the value of multiple fields of
the database table.
101 Ramesh 92 20
201 Jatin 83 19
202 Anuj 80 21
203 Monty 95 21
50
102 Saket 65 21
103 Sumit 80 21
104 Ashish 98 20
Suppose, you want to update Stu_Marks and Stu_Age of that student whose Stu_Id is
103 and 202. To do this, you have to write the following DML Update command:
1. UPDATE Student SET Stu_Marks = 80, Stu_Age = 21 WHERE Stu_Id = 103 AND
Stu_Id = 202;
This command of Data Manipulation Language does not delete the stored data
permanently from the database. We use the WHERE clause with the DELETE command
to select specific rows from the table.
P101 Chips 20 20
P102 Chocolates 60 40
P103 Maggi 75 5
P201 Biscuits 80 20
P203 Namkeen 40 50
51
Suppose, you want to delete that product from the Product table whose Product_Id is
P203. To do this, you have to write the following DML DELETE command:
Example 2: This example describes how to delete the multiple records or rows
from the database table.
101 Ramesh 92 20
201 Jatin 83 19
202 Anuj 85 19
203 Monty 95 21
102 Saket 65 21
103 Sumit 78 19
104 Ashish 98 20
Suppose, you want to delete the record of those students whose Marks is greater than
70. To do this, you have to write the following DML Update command:
52