9 Structured Query Language (SQL) : in This
9 Structured Query Language (SQL) : in This
9 Structured Query Language (SQL) : in This
C
Structured Query
9 Language (SQL)
In this Chapter
»» Introduction
»» Structured Query Language
(SQL) 9.1 Introduction
»» Data Types and
Constraints in MySQL
We have learnt about Relational Database
Management Systems (RDBMS) and its purpose
»» SQL for Data Definition
in the previous chapter. There are many
»» SQL for Data RDBMS such as MySQL, Microsoft SQL Server,
Manipulation PostgreSQL, Oracle, etc. that allow us to create
»» SQL for Data Query a database consisting of relations. These RDBMS
»» Data Updation and also allow us to store, retrieve and manipulate
Deletion data on that database through queries. In this
»» Functions in SQL chapter, we will learn how to create, populate and
»» GROUP BY Clause in SQL
query databases using MySQL.
»» Operations on Relations
9.2 Structured Query Language (SQL)
»» Using Two Relations in a
Query One has to write application programs to access
data in case of a file system. However, for database
management systems there are special kinds of
languages called query language that can be used
to access and manipulate data from the database.
The Structured Query Language (SQL) is the most
popular query language used by major relational
2021–22
2021–22
2021–22
2021–22
2021–22
Table 9.4 Data types and constraints for the attributes of relation GUARDIAN
Attribute Name Data expected to be stored Data type Constraint
GUID Numeric value consisting of 12 digit Aadhaar CHAR (12) PRIMARY KEY
number
GName Variant length string of maximum 20 VARCHAR(20) NOT NULL
characters
GPhone Numeric value consisting of 10 digits CHAR(10) NULL UNIQUE
GAddress Variant length String of size 30 characters VARCHAR(30) NOT NULL
Table 9.5 Data types and constraints for the attributes of relation ATTENDANCE.
Attribute Name Data expected to be stored Data type Constraint
AttendanceDate Date value DATE PRIMARY KEY*
RollNumber Numeric value consisting of maximum 3 INT PRIMARY KEY*
digits FOREIGN KEY
AttendanceStatus ‘P’ for present and ‘A’ for absent CHAR(1) NOT NULL
*means part of composite primary key.
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
Example 9.19
a) Let us now add a new column Commission to the
SALE table. The column Commission should have
a total length of 7 in which 2 decimal places to
be there.
mysql> ALTER TABLE SALE ADD(Commission
Numeric(7,2));
Query OK, 6 rows affected (0.34 sec)
Records: 6 Duplicates: 0 Warnings: 0
b) Let us now calculate commission for sales agents as
12% of the SalePrice, Insert the values to the newly
added column Commission and then display records
of the table SALE where commission > 73000.
mysql> UPDATE SALE SET
Commission=12/100*SalePrice;
Query OK, 6 rows affected (0.06 sec)
Rows matched: 6 Changed: 6 Warnings: 0
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
SUM(column) Returns the sum of the values mysql> SELECT SUM(Price) FROM
for the specified column. INVENTORY;
Output:
4608733.00
COUNT(*) Returns the number of records mysql> SELECT COUNT(*) from
in a table. MANAGER;
+----------+
Note: In order to display the | count(*) |
+----------+
number of records that matches
| 4 |
a particular criteria in the table, +----------+
we have to use COUNT(*) with
WHERE clause. 1 row in set (0.00 sec)
Example 9.22
a) Display the total number of records from table
INVENTORY having a model as VXI.
mysql> SELECT COUNT(*) FROM INVENTORY WHERE
Model=”VXI”;
+----------+
| COUNT(*) |
+----------+
| 2 |
+----------+
1 row in set (0.00 sec)
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
2021–22
Summary
• Database is a collection of related tables. MySQL
is a ‘relational’ DBMS.
• DDL (Data Definition Language) includes SQL
statements such as, Create table, Alter table and
Drop table.
• DML (Data Manipulation Language) includes SQL
statements such as, insert, select, update and
delete.
• A table is a collection of rows and columns, where
each row is a record and columns describe the
feature of records.
• ALTER TABLE statement is used to make changes
in the structure of a table like adding, removing
or changing datatype of column(s).
• UPDATE statement is used to modify existing
data in a table.
• WHERE clause in SQL query is used to enforce
condition(s).
• DISTINCT clause is used to eliminate repetition
and display the values only once.
2021–22
Exercise
1. Answer the following questions:
a) Define RDBMS. Name any two RDBMS software.
b) What is the purpose of the following clauses in a
select statement?
i) ORDER BY
ii) GROUP BY
c) Site any two differences between Single Row Functions
and Aggregate Functions.
d) What do you understand by Cartesian Product?
e) Differentiate between the following statements:
2021–22
2021–22
2021–22
2021–22
2021–22