Hsslive-CS-chapt-9-Structured-Query-Language
Hsslive-CS-chapt-9-Structured-Query-Language
Chapter 9
Structured Query Language
Structured Query Language (SQL) is a language designed for managing data in RDBMS. It
provides facilities to create a table, insert data into a table, retrieve information from a
table, modify data in the table, delete the existing data from a table, modify the structure of
a table, remove a table from a database, etc.
Components of SQL: Data Definition Language (DDL), Data Manipulation language (DML)
and Data Control Language (DCL).
DDL commands are used to create, modify and remove the database objects such as tables,
views and keys. Eg: CREATE TABLE, ALTER TABLE, DROP TABLE, CREATE VIEW, DROP VIEW.
DML permits users to insert data into tables, retrieve existing data, delete data from tables
and modify the stored data. Eg: INSERT INTO, SELECT, UPDATE, DELETE FROM.
DCL includes commands that control a database, including administering privileges and
committing data. Eg: GRANT, REVOKE.
SQL Data Types: INT or INTEGER, DEC or DECIMAL, CHAR or CHARACTER, VARCHAR, DATE, TIME.
DEC(5,2) or DECIMAL(5,2) denotes that the column with this specification can store any value
having a maximum of five digits, out of which two are after the decimal point.
CHAR is a fixed length character data type. It is mainly used when the data in a column are of
the same fixed length and small in size. VARCHAR represents variable length strings. The
space allocated for the data depends only on the actual size of the string, not on the
declared size of the column.
SQL Commands
Command
Syntax Optional Purpose
and use
CREATE TABLE CREATE TABLE tbl_name Constraints – The
(To create a (col_name data type <constraint>, rules enforced on
table) col_name data type <constraint>, data that are
……………………… entered into the
col_name data type <constraint>); column of a table. 1. To uniquely identify a
1. PRIMARY KEY row of a table.
2. AUTO_INCREMENT 2. To assign serial
3. NOT NULL numbers automatically.
4. UNIQUE 3. To avoid null value.
4. To avoid duplication.
5. DEFAULT
5. To set a default value.
ALTER TABLE ALTER TABLE tbl_name DROP column Instead of ADD / MODIFY
(To change ADD / MODIFY use DROP to remove an
the structure col_name data type <constraint>; existing column.
of a table) (To add a new column or modify RENAME TO To change the name of a
an existing column in a table) new_tbl_name; table.
1
Joy John’s CS capsules
Computer Science – XII Chapter 9
Additional Commands
Command Purpose
CREATE DATABASE db_name; To create a new database.
USE db_name; To open a database to perform operation on tables.
DESCRIBE tbl_name; To display the structure of a table.
SHOW TABLES; To list the tables in the current database.
2
Joy John’s CS capsules
Computer Science – XII Chapter 9
Nested Query
When we use SELECT command with WHERE clause, the condition may be framed with
another SELECT query.
For example, the following statement gives the details of students who got highest marks:
SELECT * FROM Student
WHERE Marks = (SELECT MAX(Marks) FROM Student);
The following statement gives the details of employees who get lowest salary:
SELECT * FROM Employee
WHERE Salary = (SELECT MIN(Salary) FROM Employee);
View: It is a virtual table that does not really exist in the database, but is derived from one
or more tables. A view can be created with the DDL command CREATE VIEW.
CREATE VIEW <view_name>
AS SELECT <columns> FROM <table_name>
3
Joy John’s CS capsules