Ahmed Ali M
Ahmed Ali M
8 Introduction to
Structured Query
Language (SQL)
8.1 Introduction
In this chapter
��Introduction
��Structured Query
Language (SQL)
��Data Types and
Constraints in MySQL
��SQL for Data Definition
��SQL for Data
Manipulation
��SQL for Data Query
��Data Updation and
Deletion
� E. F. Codd
Informatics Practices � Class XI
144
Activity 8.1
Explore LibreOffice
Base and compare it
with MySQL
Introduction to Structured Query Language (SQL)
145
Data type
Description
CHAR(n)
Specifies character type data of length n where n could be any value from 0 to
255. CHAR is of fixed length, means, declaring CHAR (10) implies to reserve
spaces for 10 characters. If data does not have 10 characters (for example,
�city� has four characters), MySQL fills the remaining 6 characters with spaces
padded on the right.
VARCHAR(n)
Specifies character type data of length �n� where n could be any value from 0
to 65535. But unlike CHAR, VARCHAR is a variable-length data type. That is,
declaring VARCHAR (30) means a maximum of 30 characters can be stored
but the actual allocated bytes will depend on the length of entered string. So
�city� in VARCHAR (30) will occupy the space needed to store 4 characters only.
Install sql.png
INT
INT specifies an integer value. Each INT value occupies 4 bytes of storage. The
range of values allowed in integer type are -2147483648 to 2147483647. For
values larger than that, we have to use BIGINT, which occupies 8 bytes.
FLOAT
Holds numbers with decimal points. Each FLOAT value occupies 4 bytes.
DATE
The DATE type is used for dates in 'YYYY-MM-DD' format. YYYY is the 4 digit
year, MM is the 2 digit month and DD is the 2 digit date. The supported range
is '1000-01-01' to '9999-12-31'.
8.3.2 Constraints
Constraint
Description
NOT NULL
Ensures that a column cannot have NULL values where NULL means missing/
unknown/not applicable value.
UNIQUE
DEFAULT
PRIMARY KEY
The column which can uniquely identify each row or record in a table.
FOREIGN KEY
The column which refers to value of an attribute defined as primary key in another
table.
8.4 SQL for Data Definition
Database changed
Syntax:
Show
Activity 8.3
Table 8.3, 8.4 and 8.5 show the chosen data type and
constraint for each attribute of the relations STUDENT,
GUARDIAN and ATTENDANCE, respectively.
Table 8.3 Data types and constraints for the attributes of relation STUDENT
Attribute Name
Data type
Constraint
RollNumber
INT
PRIMARY KEY
SName
VARCHAR(20)
NOT NULL
SDateofBirth
Date value
DATE
NOT NULL
GUID
FOREIGN KEY
Table 8.4 Data types and constraints for the attributes of relation GUARDIAN
Attribute Name
Data type
Constraint
GUID
CHAR (12)
PRIMARY KEY
GName
VARCHAR(20)
NOT NULL
GPhone
CHAR(10)
NULL UNIQUE
GAddress
VARCHAR(30)
NOT NULL
Table 8.5 Data types and constraints for the attributes of relation ATTENDANCE.
Attribute Name
Data type
Constraint
AttendanceDate
Date value
DATE
PRIMARY KEY*
RollNumber
INT
PRIMARY KEY*
FOREIGN KEY
AttendanceStatus
CHAR(1)
NOT NULL
Syntax:
DESCRIBE tablename;
+--------------+-------------+------+-----+---------+-------+
+--------------+-------------+------+-----+---------+-------+
+--------------+-------------+------+-----+---------+-------+
+------------------------------+
| Tables_in_studentattendance |
+------------------------------+
| student |
+------------------------------+
Syntax:
Activity 8.4
Syntax:
Syntax:
Syntax:
Syntax:
Note: We have to specify the data type of the attribute along with
constraint NOT NULL while using MODIFY.
Activity 8.5
Note: We have to specify the data type of the attribute along with
DEFAULT while using MODIFY.
Notes
Introduction to Structured Query Language (SQL)
Cautions:
Notes
Informatics Practices � Class XI
GUID
GName
GPhone
GAddress
444444444444
Amit Ahuja
5711492685
111111111111
Baichung Bhutia
7110047139
101010101010
Himanshu Shah
9818184855
333333333333
Danny Dsouza
466444444666
Sujata P.
7802983674
+--------------+-----------------+------------+-------------------------------+
+--------------+-----------------+------------+-------------------------------+
+--------------+-----------------+------------+-------------------------------+
Syntax:
Activity 8.6
+--------------+--------------+------------+----------------------------------+
+--------------+--------------+------------+----------------------------------+
+--------------+--------------+------------+----------------------------------+
RollNumber
SName
SDateofBirth
GUID
Atharv Ahuja
2003-05-15
444444444444
Daizy Bhutia
2002-02-28
111111111111
Taleem Shah
2002-02-28
4
John Dsouza
2003-08-18
333333333333
Ali Shah
2003-07-05
101010101010
Manika P.
2002-03-10
466444444666
OR
+------------+--------------+--------------+--------------+
+------------+--------------+--------------+--------------+
+------------+--------------+--------------+--------------+
1 row in set (0.00 sec)
+------------+--------------+--------------+--------------+
+------------+--------------+--------------+--------------+
+------------+--------------+--------------+--------------+
+------------+--------------+--------------+--------------+
+------------+--------------+--------------+--------------+
+------------+--------------+--------------+--------------+
Activity 8.7
Syntax:
FROM table_name
WHERE condition
+--------------+--------------+
| SName | SDateofBirth |
+--------------+--------------+
+--------------+--------------+
EmpNo
Ename
Salary
Bonus
Deptld
101
Aaliya
10000
234
D02
102
Kritika
60000
123
D01
103
Shabbir
45000
566
D01
104
Gurpreet
19000
565
D04
105
Joseph
34000
875
D03
106
Sanya
48000
695
D02
107
Vergese
15000
D01
108
Nachaobi
29000
D05
109
Daribha
42000
D04
110
Tanya
50000
467
D05
| EmpNo |
+-------+
| 101 |
| 102 |
| 103 |
| 104 |
| 105 |
| 106 |
| 107 |
| 108 |
| 109 |
| 110 |
+-------+
+-------+----------+
| EmpNo | Ename |
+-------+----------+
| 101 | Aaliya |
| 102 | Kritika |
| 103 | Shabbir |
| 104 | Gurpreet |
| 105 | Joseph |
| 106 | Sanya |
| 107 | Vergese |
| 108 | Nachaobi |
| 109 | Daribha |
| 110 | Tanya |
+-------+----------+
Notes
Introduction to Structured Query Language (SQL)
+----------+
| Name |
+----------+
| Aaliya |
| Kritika |
| Shabbir |
| Gurpreet |
| Joseph |
| Sanya |
| Vergese |
| Nachaobi |
| Daribha |
| Tanya |
+----------+
+----------+-----------+
| Name | Salary*12 |
+----------+-----------+
| Aaliya | 120000 |
| Kritika | 720000 |
| Shabbir | 540000 |
| Gurpreet | 228000 |
| Joseph | 408000 |
| Sanya | 576000 |
| Vergese | 180000 |
| Nachaobi | 348000 |
| Daribha | 504000 |
| Tanya | 600000 |
+----------+-----------+
+----------+---------------+
+----------+---------------+
| Aaliya | 120000 |
| Kritika | 720000 |
| Shabbir | 540000 |
| Gurpreet | 228000 |
| Joseph | 408000 |
| Sanya | 576000 |
| Vergese | 180000 |
| Nachaobi | 348000 |
| Daribha | 504000 |
| Tanya | 600000 |
+----------+---------------+
Notes
Informatics Practices � Class XI
Note:
+--------+
| DeptId |
+--------+
| D02 |
| D01 |
| D04 |
| D03 |
| D05 |
+--------+
+--------+
| Salary |
+--------+
| 60000 |
| 45000 |
| 15000 |
+--------+
Notes
Introduction to Structured Query Language (SQL)
Example 8.4 Display all the employees who are earning more
than 5000 and work in department with DeptId D04.
mysql> SELECT *
+-------+----------+--------+-------+--------+
+-------+----------+--------+-------+--------+
+-------+----------+--------+-------+--------+
mysql> SELECT *
+-------+----------+--------+-------+--------+
+-------+----------+--------+-------+--------+
+-------+----------+--------+-------+--------+
+----------+--------+
| Ename | DeptId |
+----------+--------+
| Shabbir | D01 |
| Joseph | D03 |
| Sanya | D02 |
| Nachaobi | D05 |
| Daribha | D04 |
| Tanya | D05 |
+----------+--------+
Activity 8.8
SELECT *
FROM EMPLOYEE
+----------+--------+
| Ename | DeptId |
+----------+--------+
| Shabbir | D01 |
| Joseph | D03 |
| Sanya | D02 |
| Nachaobi | D05 |
| Daribha | D04 |
| Tanya | D05 |
+----------+--------+
mysql> SELECT *
+-------+----------+--------+-------+--------+
+-------+----------+--------+-------+--------+
+-------+----------+--------+-------+--------+
mysql> SELECT *
+-------+----------+--------+-------+--------+
+-------+----------+--------+-------+--------+
+-------+----------+--------+-------+--------+
Notes
Introduction to Structured Query Language (SQL)
mysql> SELECT *
+-------+----------+--------+-------+--------+
+-------+----------+--------+-------+--------+
+-------+----------+--------+-------+--------+
mysql> SELECT *
+-------+----------+--------+-------+--------+
+-------+----------+--------+-------+--------+
mysql> SELECT *
Notes
Informatics Practices � Class XI
+-------+----------+--------+-------+--------+
+-------+----------+--------+-------+--------+
+-------+----------+--------+-------+--------+
mysql> SELECT *
+-------+----------+--------+-------+--------+
| EmpNo | Ename | Salary | Bonus | DeptId |
+-------+----------+--------+-------+--------+
+-------+----------+--------+-------+--------+
+----------+
| EName |
+----------+
| Aaliya |
| Kritika |
| Shabbir |
| Gurpreet |
| Joseph |
| Sanya |
| Tanya |
+----------+
Activity 8.9
SELECT *
FROM EMPLOYEE
ORDER BY Salary,
Bonus;
SELECT *
FROM EMPLOYEE
ORDER BY Salary,Bonus
desc;
Introduction to Structured Query Language (SQL)
mysql> SELECT *
+-------+---------+--------+-------+--------+
+-------+---------+--------+-------+--------+
+-------+---------+--------+-------+--------+
mysql> SELECT *
+-------+---------+--------+-------+--------+
+-------+---------+--------+-------+--------+
| 101 | Aaliya | 10000 | 234 | D02 |
+-------+---------+--------+-------+--------+
mysql> SELECT *
Notes
Informatics Practices � Class XI
+-------+-------+--------+-------+--------+
+-------+-------+--------+-------+--------+
+-------+-------+--------+-------+--------+
+---------+
| Ename |
+---------+
| Joseph |
| Vergese |
+---------+
+----------+
| EName |
+----------+
| Aaliya |
| Sanya |
| Nachaobi |
| Daribha |
| Tanya |
+----------+
Syntax:
UPDATE table_name
WHERE condition;
+------------+---------------+----------+------------------------------------+
+------------+---------------+----------+------------------------------------+
+------------+---------------+----------+------------------------------------+
Syntax:
WHERE condition;
Informatics Practices � Class XI
+------------+--------------+--------------+--------------+
+------------+--------------+--------------+--------------+
+------------+--------------+--------------+--------------+
Summary
Notes
Exercise
ALTER
UPDATE
Restrictions on columns
DELETE
Table definition
INSERT INTO
CONSTRAINTS
DESC
Delete an existing row from a table
CREATE
Create a database
name CHAR(30),
Informatics Practices � Class XI
student_id INT,
gender CHAR(1),
);
VALUES (�Suhana�,109,�F�),
VALUES (�Rivaan�,102,�M�),
VALUES (�Atharv�,103,�M�),
VALUES (�Rishika�,105,�F�),
VALUES (�Garvit�,104,�M�),
VALUES (�Shaurya�,109,�M�);
i) Error
ii) No Error
iii) Depends on compiler
iv) Successful completion of the query
DELETE student
WHERE student_id=109;
i) 1 row
ii) All the rows where student ID is equal to 109
iii) No row will be deleted
iv) 2 rows
Notes
Introduction to Structured Query Language (SQL)
MovieID
MovieName
Category
ReleaseDate
ProductionCost
BusinessCost
001
Hindi_Movie
Musical
2018-04-23
124500
130000
002
Tamil_Movie
Action
2016-05-17
112000
118000
003
English_Movie
Horror
2017-08-06
245000
360000
004
Bengali_Movie
Adventure
2017-01-04
72000
100000
005
Telugu_Movie
Action
100000
006
Punjabi_Movie
Comedy
30500
Table: MATCH_DETAILS
MatchID
MatchDate
FirstTeamID
SecondTeamID
FirstTeamScore
SecondTeamScore
M1
2018-07-17
90
86
M2
2018-07-18
3
45
48
M3
2018-07-19
78
56
M4
2018-07-19
56
67
M5
2018-07-20
32
87
M6
2018-07-21
67
51
h) Use the foreign key constraint in the MATCH_
DETAILS table with reference to TEAM table so
that MATCH_DETAILS table records score of teams
existing in the TEAM table only.
Table: STUDENT
RollNo
Name
Stream
Section
RegistrationID
Table: PROJECT_ASSIGNED
RegistrationID
ProjectID
AssignDate
Table: PROJECT
ProjectID
ProjectName
SubmissionDate
TeamSize
GuideTeacher