Chapter 8 - Introduction To Structured Query Language (SQL)
Chapter 8 - Introduction To Structured Query Language (SQL)
MYSQL: MySQL is a Relational Database Management System. It was originally founded and developed in
Sweden by David Axmark, Allan Larsson and Michael Widenius.
Characteristics of MySQL:
1. Cost: It is released under an open–source license and hence required no cost or payment for its usage.
2. Speed: It has superior speed. It is easy to use and is reliable.
3. Ease of Use: It is a simple database system. It can be easily managed from the command line. Several
graphical interfaces are also available.
4. Query Language Support: Standard SQL commands are understandable to MySQL.
5. Data Types: It supports many data types to support different types of data. It also supports fixed–length
and variable–length records.
6. Security: It offers privilege and password system that is very flexible and secure. Passwords are secure
because all password traffic is encrypted at the time of connecting to a server.
7. Scalability and Limits: It can handle large databases. Some real life MySQL databases contain 50
million records, some have up to 60,000 tables and about 500,00,00,000 rows.
8. Connectivity: Clients can connect to MySQL server easily
Data Types:
Commonly used data types available in MySQL are,
A normal–sized integer that can be signed or unsigned. If signed, the allowable
INT range is from –2147483648 to 2147483647. If unsigned, the allowable range is
from 0 to 4294967295. Each INT value occupies 4 bytes of storage
Represents a floating point number. M is the total number of digits, and D is the
DECIMAL(M, D)
number of digits after decimal point.
FLOAT Holds numbers with decimal points. Each float value occupies 4 bytes
DATE A date in YYYY–MM–DD format, between 1000–01–01 and 9999–12–31
A fixed–length string between 0 and 255 characters in length. M is the length.
CHAR(M)
Providing M is optional and its default value is 1
A variable–length string between 0 and 65535 characters in length. Providing M
VARCHAR(M)
is compulsory
Constraints: Constraints are certain types of restrictions on the data values that an attribute can have. They are
used to ensure the accuracy and reliability of data.
Constraint Description
Ensures that a column cannot have NULL values where NULL means
NOT NULL missing/ unknown/not applicable value.
UNIQUE Ensures that all the values in a column are distinct / unique.
PRIMARY KEY The column which can uniquely identify each row or record in a table.
The column which refers to value of an attribute defined as primary key in
FOREIGN KEY another table.
CREATING A TABLE:
A table in the database can be created using CREATE TABLE command, as follows
SQL commands are not case sensitive. For example, the command CREATE can also be provided as
Create or create etc
While giving name to a table it should be relevant to the data that it holds
2. Data Manipulation Language (DML) Commands: The Query and Update commands on tables are
referred as DML commands. A few examples for DML commands in SQL are:
a. SELECT – Extracts data from a table
b. UPDATE – Updates data in a table
c. DELETE – Deletes data from a table
d. INSERT INTO – Inserts new data into a table
3. Data Control Language (DCL) / Transaction Control Language (TCL) Commands: The commands
which are used to control the access to databases and tables are referred as Data Control Language or
Transaction Control Language commands. A few examples for TCL commands in SQL are:
a. GRANT – To give rights of transaction to a user
b. REVOKE – To take back rights of transaction from a user
c. COMMIT – To make changes permanent
d. ROLLBACK – To undo changes of transaction
VIEWING STRUCTURE OF A TABLE:
To see the structure of a table that is already created, the DESCRIBE or DESC command can be used as
follows
Syntax : DESCRIBE <TableName>; (Or) DESC <TableName>;
Ex : DESC Student;
CHANGING STRUCTURE OF A TABLE USING ALTER TABLE COMMAND:
An existing table’s structure can be changed by using ALTER TABLE command.
Adding an attribute to a Table:
Syntax : ALTER TABLE <TableName> ADD <ColumnName> DataType;
Ex : ALTER TABLE Student ADD Grade CHAR(1);
Removing an attribute of a Table:
Syntax : ALTER TABLE <TableName> DROP <ColumnName>;
Ex : ALTER TABLE Student DROP DOB;
Modifying datatype of an attribute of a Table:
Syntax : ALTER TABLE <TableName> MODIFY <ColumnName> <New_Definition>;
Ex : ALTER TABLE Student Class VARCHAR(4);
Add Primary Key constraint to a Table:
Syntax : ALTER TABLE <TableName> ADD PRIMARY KEY(ColumnName);
Ex : ALTER TABLE Student ADD PRIMARY KEY(RollNo);
Removing Primary Key from a table:
Syntax : ALTER TABLE <TableName> DROP PRIMARY KEY;
Ex : ALTER TABLE Student DROP PRIMARY KEY;
Adding Foreign Key constraint to a Table:
Syntax : ALTER TABLE <TableName> ADD FOREIGN KEY(ColumnName) REFERENCES
<Referenced_TableName>(ColumnName);
Ex : ALTER TABLE Student ADD FOREIGN KEY(RollNo) REFERENCES
Marks(RollNo);
4
RELATIONAL OPERATORS:
The relational operators used in MySQL are as follows
Operator Meaning
< Less Than
<= Less Than or Equal to
> Greater Than
>= Greater Than or Equal to
= Equal to
!= (Or) <> Not Equal to
The above query will displays the RollNo, Name, Marks1 from the Student table whose value of Marks1
values are not be between 70 and 80, both exclusive
Ex: SELECT RollNo, Name, Marks1 FROM Student WHERE Marks1 NOT IN (68, 76, 78);
Ex: SELECT * FROM Employee WHERE State NOT IN ('DELHI', 'MUMBAI', 'UP');
The keyword NOT LIKE is used to select the rows that do not match the specified pattern. To display
rows from the table Student that have names not starting with 'G', she enters:
Ex: SELECT * FROM Student WHERE Name NOT LIKE 'G%';
PRECEDENCE OF OPERATORS:
Precedence is the order in which different operators are evaluated in the same expression. When
evaluating an expression containing multiple operators, operators with higher precedence are evaluated before
evaluating those with lower precedence. Operators with equal precedence are evaluated from left to right
within the expression. Parenthesis can be used to change the preference of an operator. Various operators in
descending order of precedence (top to bottom) are listed below:
6
!
– (unary minus)
^
*, /, DIV, %, MOD
-, +
=, <=>, >=, >, <=, <, <>, !=, IS, LIKE, IN
BETWEEN,
NOT
&&, AND
||, OR
NULL:
NULL means a value that is unavailable, unassigned, unknown or inapplicable. NULL is not the same as
zero or a space or any other character. In a table NULL is searched for using IS NULL keywords.
Syntax: SELECT <column name>, [<column name>…] [WHERE <Condition list>] ORDER BY
<column name>;
Ex: SELECT * FROM Student ORDER BY Marks1;
Ex: SELECT * FROM Student ORDER BY Name;
To display data in descending order, DESC keyword is used in ORDER BY clause. However it is not
necessary to specify ASC for ascending order as it is the default order.
Ex: SELECT * FROM Student ORDER BY Marks1 DESC;
UPDATE STATEMENT:
The UPDATE statement is used to update the data of the table. WHERE clause is also applicable to this
statement.
Syntax: UPDATE <table_name> SET <column name> = <value>, [ <column name> = <value>, …]
[WHERE <condn>];
Ex: UPDATE Student SET Marks1 = 94;
The above statement sets the Column Marks1 value of all rows to 94 of the table Student. To apply this to
specific rows, WHERE clause can be applied along with UPDATE statement
Ex: UPDATE Student SET Marks1 = 94 WHERE name = 'Monica Rana';
DELETE STATEMENT:
DELETE statement is used to delete rows from a table. DELETE removes the entire row, not the
individual column values.
DELETE statement can be used to delete all rows of the table also. The following statement can be used
to delete all the rows from Student table.
CONSTRAINTS:
A constraint is a rule that is applied on the columns of tables to ensure data integrity and consistency.
1. NOT NULL Constraint:
When this constraint is set to a column, it ensures that the value ‘NULL’ cannot be entered in the
specified column in any row.
Ex: CREATE TABLE Student (RollNo INT(5) NOT NULL, Name VARCHAR(25));
2. UNIQUE Constraint:
When this constraint is set to a column, it ensures that the duplicate values cannot be entered in
the specified column.
5. ENUM Constraint:
This constraint defines a set of string values as the column domain. So any value in this column
will be from the specified values only.
Now, while inserting rows into the table Person, for the field Gender the value either ‘Male’ or
‘Female’ only is to be entered. Attempting entering value other than this will results in error
6. DEFAULT Constraint:
This constraint is used to assign a default value when no value is provided to attribute.
Now, while inserting data into the table if value for the gender is not provided then the value
‘Male’ will be assigned.