Com204 Lec6
Com204 Lec6
L06 – Introduction to
Structured Query Language
(SQL)
Database Principles: Fundamentals of Design, Implementation, and Management, 10 th Edition, Coronel, Morris, & Rob
In this lecture, you will learn:
• The basic commands and functions of SQL
• How to use SQL for data definition (to create
tables, attributes, constraints etc)
• How to use SQL for data manipulation:
CRUD(Create, Read, Update, Delete)
COM304 Databases 2
Introduction to SQL
• A standard language for storing, manipulating and retrieving data in
databases
• All dialect support at least the major commands (such as SELECT, UPDATE,
DELETE, INSERT, WHERE) in a similar manner.
COM304 Databases 3
Introduction to SQL
• SQL functions fit into two broad categories:
– Data definition language
• Commands to:
– Create database objects, such as tables, indexes, and views
– Define access rights to those database objects
– Data manipulation language
• Commands to CRUD
COM304 Databases 4
Data Definition Language (DDL)
COM304 Databases 5
Data Manipulation Language (DML)
COM304 Databases 6
Data Manipulation Language (DML)
(continued)
COM304 Databases 7
SQL Constraints
• Constraints specify the type of data that can go into a table
• NOT NULL constraint
– Ensures that column does not accept nulls
• UNIQUE constraint
– Ensures uniqueness for a column or set of columns
• PRIMARY KEY constraint
– Uniquely identifies each row in a table
– A PK constraint automatically has a UNIQUE constraint and NOT NULL
• FOREIGN KEY constraint
– Links tables.
– Field(s)/attribute(s) in one table that refers to the PRIMARY KEY in another table
• DEFAULT constraint
– Assigns value to attribute when a new row is added to table
– Eg: author_rank varchar(10) DEFAULT ‘Silver'
• CHECK constraint
– Validates data when attribute value is entered
– Eg: CHECK (Age>=18).
• AUTO INCREMENT constraint
– AUTO_INCREMENT allows a unique number to be generated automatically when a new record is
inserted into a table
• INDEX constraint
– Indexes are used to retrieve data from the database more quickly than otherwise
COM304 Databases 8
Creating Schema/Database
• SQL keywords are NOT case sensitive: select == SELECT
• Syntax
CREATE DATABASE databasename;
• Example
– CREATE DATABASE librarydb;
COM304 Databases 9
Creating Table
• Syntax
– CREATE TABLE tablename (
column1 datatype [constraint],
column2 datatype [constraint],
PRIMARY KEY (column1),
FOREIGN KEY (column2) REFERENCES tablename (column)
);
• Example
– CREATE TABLE author (
Author_id int not null
Author_fname varchar(20),
Author_lname varchar(20),
PRIMARY KEY (Author_id)
);
COM304 Databases 10
ALTER Table
• Add, delete, or modify attributes/columns in an existing table.
• Add and drop various constraints on an existing table.
• Syntax
– ALTER TABLE table_name ADD column_name datatype [constraint];
– ALTER TABLE table_name DROP COLUMN column_name;
– ALTER TABLE table_name MODIFY COLUMN column_name datatype
[constraint];
– ALTER TABLE table_name ADD PRIMARY KEY (column_name);
– ALTER TABLE table_name ADD FOREIGN KEY (column_name) REFERENCES
table_name(column_name);
• Example
– ALTER TABLE student ADD gender varchar(7) not null;
– ALTER TABLE student DROP COLUMN gender;
– ALTER TABLE student MODIFY COLUMN gender varchar(1);
– ALTER TABLE student ADD PRIMARY KEY (student_id);
– ALTER TABLE student ADD FOREIGN KEY (ParentID) REFERENCES Parent(ParentID);
COM304 Databases 11
DROP vs TRUNCATE
• The DROP DATABASE statement is used to drop an existing database.
• Syntax
DROP DATABASE databse_name;
• Example
– DROP DATABASE librarydb;
• The DROP TABLE statement is used to drop an existing table and all its data in a table.
• Syntax
DROP TABLE table_name;
• Example
– DROP TABLE student;
• The TRUNCATE TABLE statement is used to delete the data inside a table, not the table itself.
• Syntax
TRUNCATE TABLE table_name;
• Example
– TRUNCATE TABLE student;
COM304 Databases 12
INSERT/CREATE
• SYNTAX
• INSERT INTO tablename (column1, column2, …,
columnN)
VALUES (value1, value2, … , valueN);
•.
• Example
– INSERT INTO student (fname, lname) values
(‘Douglas’, ‘ Rukayyah’);
– INSERT INTO student values (‘Rukayyah’, ‘
Douglas’);
COM304 Databases 13
SELECT
• Syntax
• To select all columns in the table
– SELECT * FROM tablename;
• To select some columns
– SELECT columnlist FROM tablename;
– Columnlist represents one or more attributes, separated by
commas. Eg. fname, lname, email, phone
• Example
• To select all columns in the table
– SELECT * FROM student;
• To select some columns
– SELECT fname, lname, email FROM student;
COM304 Databases 14
WHERE
• Select partial table contents by placing restrictions
on rows to be included in output
– Add conditional restrictions to SELECT statement,
using WHERE clause
• Syntax:
– SELECT columnlist FROM table WHERE [conditionlist ] ;
• Example:
– SELECT fname, lname
FROM student WHERE department = ‘CS’;
COM304 Databases 15
Logical Operators
• AND
– Both conditions have to be true
Syntax:
– SELECT [columnlist] FROM [table] WHERE
[condition] AND [condition];
Example:
– SELECT fname, lname
FROM student WHERE department = ‘CS’ AND
fname=‘Smith’;
COM304 Databases 16
Logical Operators
• OR
– At least one condition has to be true
Syntax:
– SELECT [columnlist] FROM [table] WHERE
[condition] OR [condition];
Example:
– SELECT fname, lname
FROM student WHERE department = ‘CS’ OR
fname=‘Smith’;
COM304 Databases 17
Logical Operators
• NOT
– The condition should return false
Syntax:
– SELECT [columnlist] FROM [table] WHERE NOT
[condition];
Example:
– SELECT fname, lname
FROM student WHERE NOT department = ‘CS;
COM304 Databases 18
UPDATE
• Syntax
• UPDATE tablename
SET columnname1 = expression , columname2 = expression]
[WHERE conditionlist];
• Syntax
• UPDATE student
SET fname = ‘Fatima’ , lname = ‘Badmos’
WHERE student_id=1;
COM304 Databases 19
Deleting Table Rows
• Syntax
• DELETE FROM tablename [WHERE conditionlist ];
• Example
• DELETE FROM student WHERE student_id = 1;
COM304 Databases 20
Restoring Table Contents
• ROLLBACK
– Used to restore database to its previous condition
– Only applicable if COMMIT command has not been used to
permanently store changes in database
• Syntax:
– ROLLBACK;
• COMMIT and ROLLBACK only work with data
manipulation commands that are used to add, modify, or
delete table rows
COM304 Databases 21