SQL ( STRUCTURED QUERY
LANGUAGE )
SQL
SQL is a language that is used to manage data stored in a RDBMS.
SQL statements are not case – sensitive.
All SQL statements should end with semicolon.
It comprises of
- Data Definition Language (DDL)
- Data Manipulation Language (DML)
DDL:
DDL is a language which is used to define structure and constraints of data.
Ex. CREATE ,ALTER ,DROP , RENAME
DML:
It is used to insert, modify and delete data in a database.
Ex. UPDATE,DELETE, INSERT, SELECT
DATA TYPE
It is an attribute that specifies the type of data that the
object can hold.
SQL COMMANDS
1. Create database:
CREATE DATABASE < database_name>;
2. Show databases command that is used to see the list of databases that currently
exist on the server: SHOW DATABASES;
3. USE command is used to tell the server which database we will use for further
statements. USE
<database_name>;
4. DROP database command is used to remove a database.
DROP DATABASE<database_name>;
DDL commands
1. Create table
CREATE TABLE <table name>(
<Column 1><data type>[constraint],
<Column 1><data type>[constraint]
);
2. Show tables command displays all the table created in the current database.
SHOW TABLES;
3. Describe command is used to view structure of table.
DESC < Table_ name >;
4. DROP table command is used to delete table.
DROP TABLE <table _ name >;
5. Alter table command is used to modify the base table definition.
ADD column: ALTER TABLE <table_name> ADD <col><data type>;
Delete col: ALTER TABLE <table_name> DROP <column name>;
Delete primary key: ALTER TABLE <table_name> DROP Primary key;
Delete Default: ALTER TABLE <table_name> ALTER <COL> DROP Default;
Delete Constraints: ALTER TABLE <table_name>
DROP <constraint_type> constraint_name;
Modify data type: ALTER TABLE <table_name> Modify column <column_name> <data type>;
DATABASE CONSTRAINTS
1. NOT NULL: An attribute value may not be permitted to be NULL.
Ex. CREATE TABLE TEACHER
(
Teacher_ID INTEGER,
First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2),
Date_of_Birth DATE,
Dept_No INTEGER
);
2. DEFAULT : If a user has not entered a value for an attribute, then default value specified while
creating the table is used.
CREATE TABLE TEACHER
(
Teacher_ID INTEGER,
First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2) DEFAULT 40000,
Date_of_Birth DATE,
Dept_No INTEGER
);
3. CHECK: In order to restrict the values of an attribute within a range, CHECK constraint may be used.
- MySQL does not support CHECK constraint.
CREATE TABLE TEACHER
(
Teacher_ID INTEGER,
First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2) DEFAULT 40000,
Date_of_Birth DATE,
Dept_No INTEGER CHECK (Dept_No<=110)
);
4. KEY Constraint: Primary Key of a table can be specified in two ways.
a. If the primary key of the table consist of a single attribute, then the corresponding attribute
can be declared primary key along with its description.
CREATE TABLE TEACHER
(
Teacher_ID INTEGER PRIMARY KEY,
First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2) DEFAULT 40000,
Date_of_Birth DATE,
Dept_No INTEGER
);
b. if primary key contains more than one attribute then it must be specified separately as a list
of attributes it comprises of, within parenthesis, separated by commas.
CREATE TABLE TEACHER
(
Teacher_ID INTEGER,
First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2) DEFAULT 40000,
Date_of_Birth DATE,
Dept_No INTEGER,
PRIMARY KEY (Teacher_ID, Date_of_Birth)
);
5. REFERENTIAL INTEGRITY CONSTRAINT- This constraint is specified by using the foreign key
clause. This clause contains the foreign key and the primary key referred to by this foreign key
along with the name of the relation.
MUL(in structure) implies multiple occurrences of given value can appear within the column.
This is because it is a foreign key.
Department Table
CREATE TABLE Department
(
Dept_ID INTEGER PRIMARY KEY,
Dept_Name VARCHAR(20) NOT NULL
);
TEACHER TABLE
CREATE TABLE Teacher
(
Teacher_ID INTEGER PRIMARY KEY,
First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20),
Gender CHAR(1),
Salary DECIMAL(10,2) DEFAULT 40000,
Date_of_Birth DATE,
Dept_No INTEGER,
FOREIGN KEY (Dept_No) REFERENCES
Department(Dept_ID)
);
The action can be taken in case the foreign key attribute or the primary key
attribute value are changed as that may result in violation of the referential
integrity constraint.
This may be done using the following commands:
1. ON DELETE
2. ON UPDATE
Actions that can be taken are as follows:
1. SET NULL: It will set the value of the foreign key as NULL, if the
corresponding value of primary key is deleted or updated.
FOREIGN KEY (Dept_No) REFERENCES Department (Dept_ID) ON
DELETE SET NULL
ON UPDATE SET NULL
);
2. CASCADE: It will perform same operation on the FK as performed on the corresponding value of
primary key.
FOREIGN KEY (Dept_No) REFERENCES Department (Dept_ID)
ON DELETE CASCADE
ON UPDATE CASCADE
);
3. RESTRICT: It will reject the operation on primary key, if it is referred by a foreign key.
FOREIGN KEY (Dept_No) REFERENCES Department (Dept_ID)
ON DELETE RESTRICT
ON UPDATE RESTRICT
);
NAMING CONSTRAINTS
Constraints can be named in the Create Table command. The Advantage is that
named constraints can be easily deleted or updated using the Alter Table
command.
CREATE TABLE Teacher
CONSTRAINT TEACHER_PK PRIMARY KEY
(
(Teacher_ID),
Teacher_ID INTEGER, CONSTRAINT TEACHER_FK FOREIGN KEY
First_Name VARCHAR(20) NOT NULL, (Dept_No) REFERENCES
Last_Name VARCHAR(20), Department(Dept_ID) ON DELETE SET NULL ON
Gender CHAR(1), UPDATE SET NULL
Salary DECIMAL(10,2) DEFAULT 40000, );
Date_of_Birth DATE,
Dept_No INTEGER,
DML COMMANDS
1.Insert command: This command is used to insert a tuple in a relation.
CREATE TABLE Teacher
(Teacher_ID INTEGER, First_Name VARCHAR(20) NOT NULL, Last_Name
VARCHAR(20), Gender CHAR(1), Salary DECIMAL(10,2) DEFAULT 40000,
Date_of_Birth DATE, Dept_No INTEGER, CONSTRAINT TEACHER_PK PRIMARY
KEY (Teacher_ID));
To insert a tuple in the Teacher table, INSERT command can be used as shown below:
INSERT INTO Teacher
VALUES (101,"Shanaya", "Batra", 'F', 50000, '1984-08-11', 1);
Update command:
This command is used to update the attribute values of one or more tuples in a table.
UPDATE Teacher SET Salary=55000
WHERE Teacher_ID=101;
Delete command: In order to delete one or more tuples, DELETE command is used.
DELETE FROM Teacher
WHERE Teacher_ID=101;
Select command: The SELECT Command is used to retrieve information from a database.
Syntax of SELECT Command is as follows:
SELECT <attribute list>
FROM <table list>
WHERE <condition>
Ex. Select First_Name from TEACHER where Dept_No = 6;