You are on page 1of 9

SQL Basic Commands Tutorial

ISYS3414 Practical Database Concepts

1
(Main) SQL Commands
• SQL-Data Definition Language
oTo manage tables and integrity restrictions

CREATE TABLE: creation of a new table and its restrictions


DROP TABLE: deletion of an existing table (including its data)
ALTER TABLE: modification of columns and restrictions of a table
CREATE VIEW: creation of a view of data in the existing tables

2
(Main) SQL Commands
• SQL-Data Manipulation Language
oTo manage data in tables

INSERT INTO: insertion of new rows in a table


UPDATE: update of data in a table’s row
DELETE FROM : deletion of rows in a table
SELECT: data query in one or more tables

3
SQL – Table Creation

• Given a relation schema:


oEx.: students(sid: integer, gname: string, fname:
string, age: integer, gpa: real)
• The CREATE TABLE statement creates an empty table
oEx.: CREATE TABLE students (
sid INTEGER,
gname VARCHAR(30),
fname VARCHAR(30),
age INTEGER,
gpa REAL
);
4
SQL – Table Deletion

• The DROP TABLE statement deletes an existing table


and its contents.
oEx.:

DROP TABLE students;

5
SQL – Data Insertion
• The INSERT INTO statement inserts a row in a table
o Ex.:

INSERT INTO students (sid, gname, fname, age, gpa)


VALUES (123456, 'Donald', 'Trump', 23, 0.1);

sid gname fname age gpa Note: In this running


682634 John Smith 20 3.0 example you may insert
students with the same
632461 Phu Nguyen 21 1.0
sid because we did not
612352 Thong Nguyen 19 2.7 add any integrity
603111 Tam Quach 20 0.8 restriction to this table.
123456 Donald Trump 23 0.1
6
SQL – Update data
• The UPDATE statement updates rows in a table
o Ex.: Increase by 50% the gpa of all the students with a gpa less than 1

UPDATE students S
SET S.gpa = S.gpa * 1.5
WHERE S.gpa < 1;
sid gname fname age gpa sid gname fname age gpa
682634 John Smith 20 3.0 682634 John Smith 20 3.0
632461 Phu Nguyen 21 1.0 632461 Phu Nguyen 21 1.0
612352 Thong Nguyen 19 2.7 612352 Thong Nguyen 19 2.7
603111 Tam Quach 20 0.8 603111 Tam Quach 20 1.2
123456 Donald Trump 23 0.1 123456 Donald Trump 23 0.15
7
SQL – Delete data

• The DELETE FROM statement deletes rows in a table


oEx.: Delete all the students with family name “Nguyen”
DELETE FROM students
WHERE fname = 'Nguyen';

sid gname fname age gpa sid gname fname age gpa
682634 John Smith 20 3.0 682634 John Smith 20 3.0
632461 Phu Nguyen 21 1.0 603111 Tam Quach 20 0.8
612352 Thong Nguyen 19 2.7 123456 Donald Trump 23 0.1
603111 Tam Quach 20 0.8
123456 Donald Trump 23 0.1
8
Q &A

You might also like