Lecture5-SQL1
Lecture5-SQL1
https://www.tutorialspoint.com
− Dr. Edgar F. "Ted" Codd of IBM is known
as the father of relational databases. He
described a relational model for − IBM worked to develop Codd's ideas and
databases. released a product named System/R.
1970 1978
1974 1986
A BRIEF
▪ SQL-86
▪ SQL-89 (minor revision)
DEMO ON SQLITE
https://www.db-
book.com/university-
lab-dir/sqljs.html
Create Create database …
USE DatabaseName:
• USE testDB;
CREATING TABLE
The basic syntax of the CREATE TABLE statement is
as follows:
); all attributes
DESTROYING
RELATION/TABLE
Syntax:
Drop table Table_name
ADDING AND INSERT INTO Students (sid, name, login, age, gpa)
VALUES (53688, ‘Smith’, ‘smith@ee’, 18, 3.2)
TUPLES DELETE
FROM Students S
WHERE S.name = ‘Smith’
INSERT INTO CUSTOMERS (ID, NAME, AGE, ADDRESS, SALARY)
VALUES (7, 'Muffy', 24, 'Indore', 10000.00);
Primary Key
Primary key attributes are underlined in a schema
Person(pid, address, name)
Person2(address, name, age, job)
Super Key
Foreign Key
PRIMARY KEY
CONSTRAINTS
Key = subset of columns that uniquely identifies tuple
Another constraint on the table
no two tuples can have the same values for those
columns
Examples:
Movie(title, year, length, genre): key is (title, year)
what is a good key for Student?
Students(sid: string, name: string, login:
string, age: integer, gpa: real).
Can have multiple keys for a table
Only one of those keys may be “primary”
DBMS often makes searches by primary key fastest
other keys are called “secondary”
• Possibly many candidate keys
– specified using UNIQUE
– one of which is chosen as the primary key.
Example:
CREATE TABLE Enrolled (sid
“For a given student and course, there is a CHAR(10)
single grade.” cid CHAR(20),
What a primary key is in a table? grade CHAR(2), PRIMARY
KEY ???)
PRIMARY AND
CANDIDATE KEYS IN SQL
Possibly many candidate keys
specified using UNIQUE
one of which is chosen as the primary key.
“For a given student and course, there is a single
grade.”
• “Students can take only one course, and CREATE TABLE Enrolled
vs. receive a single grade for that course; (sid CHAR(10)
further, no two students in a course cid CHAR(20),
receive the same grade.” grade CHAR(2),
PRIMARY KEY ???, UNIQUE ??? )
CREATE TABLE Enrolled
• Possibly many candidate keys
( sid CHAR(10)
– specified using UNIQUE
cid CHAR(20),
– one of which is chosen as the primary key. grade CHAR(2),
• “For a given
vs. student and course, there is a PRIMARY KEY (sid, cid)
single grade.” vs
CREATE TABLE Enrolled
• “Students can take only one course, and ( sid CHAR(10)
receive a single grade for that course; cid CHAR(20),
further, no two students in a course grade CHAR(2),
PRIMARY KEY sid,
receive the same grade.” UNIQUE (cid, grade))
• Possibly many candidate keys
– specified using UNIQUE CREATE TABLE Enrolled
( sid CHAR(10)
– one of which is chosen as the primary key.
cid CHAR(20),
• “For a given student and course, there is a grade CHAR(2),
single grade.” vs PRIMARY KEY (sid, cid)
• “Studentsvs.
can take only one course, and receive
CREATE TABLE Enrolled
a single grade for that course; further, no two
( sid CHAR(10)
students in a course receive the same grade.” cid CHAR(20),
• Used carelessly, an IC can prevent the storage grade CHAR(2),
of database instances that arise in practice! PRIMARY KEY sid,
UNIQUE (cid, grade))
Foreign key: Set of fields in one Must correspond to primary key of the
relation that is used to 'refer' to second relation
Like a 'logical pointer'
a tuple in another relation
Default is No action:
(delete/update is
rejected)
Then delete all
Delete the 53666 tuples from
tuple from Enrolled that
Students: have sid =
’53666’.