Basic SQL
Basic SQL
1
SQL stands for Structured Query Language, SQL is standard language by which
the various operation can be performed on data stored in a relational database, the
operations like storing, manipulating and retrieving data.
With the help of SQL Commands the various tasks can be performed, like a create
a table, add data to tables, drop the table, modify the table, set permission for
users.
SQL commands are instructions, which is used to communicate with the database.
It is also used to perform specific tasks, functions and queries of data.
SQL uses compact english like statements and performs very complex jobs to
access information from very large sized databases.
2
Data Definition Language (DDL)
3
Create Database / Table
CREATE DATABASE adm_db
DROP DATABASE <database name>
The following statement in the SQL language defines the student table:
create table student(stu_id integer,
stu_name char(30),
age integer,
course char(5))
rename TABLE form_in to form_input
4
Alter Table
The following statements in the SQL language defines the alter
student table:
5
Drop Table
The following statement in the SQL language defines the drop
student table:
6
Data Manipulation Language (DML)
DML is used
• To retrieve data from the table,
• To insert data into a table
• To update data in the table
• To delete data from the table
It is the following commands:
• select
• insert
• update
• delete
7
SELECT
Select * from form_input
SELECT form_no,stu_name FROM form_input
Select * from stu where form_no=1234
SELECT * FROM form_input WHERE course_code=2
8
INSERT
INSERT INTO form_input(form_no, stu_name, age, course, f_name)
VALUES (1234,'Amar Sahu',25,‘MCA',‘M. Sahu')
9
UPDATE
UPDATE `form_input`
SET `stu_name`= 'Amar Singh' WHERE `form_no`=1237
UPDATE `form_input`
SET `course_code`= 3 WHERE `course`=‘MCA’ and `session`=‘2020-21’
UPDATE `course`
SET `course_code`= 2 WHERE `course_name`=‘M.B.A‘
10
Create table course & Insert data into course table
create table course(course_code integer,
course_name char(6),course_full_name char(30))
INSERT INTO `course`(`course_code`, `course_name`, `course_full_name`)
VALUES (1,'B.C.A.','Bachelor of Computer Application')
INSERT INTO `course`(`course_code`, `course_name`, `course_full_name`)
VALUES (2,'B.B.A.','Bachelor of Business Administration')
INSERT INTO `course`(`course_code`, `course_name`, `course_full_name`)
VALUES (4,'M.C.A.','Master of Computer Application')
11
Querying Multiple Tables – Retrieve data from two related tables
SELECT * FROM form_input, course WHERE course.course_code=form_input.course_code
12
Querying Multiple Tables – Retrieve some specific data(i.e. form_no, stu_name &
course_name) from two related tables
SELECT a.form_no,a.stu_name,b.course_name FROM form_input a, course b WHERE
a.course_code=b.course_code
13
References:
•North, Silbertz, Sudarshan, “Database Concepts”, Tata Mcgraw-hill Education
(India) Pvt. Ltd.
•Date C J, “An Introduction To Database System”, Addision Wesley
• Jain, Pillai, Singh “Introduction to Database Management”, BPB Publications.
14