ch06 BasicSQL
ch06 BasicSQL
Introduction to SQL:
Structured Query Language
Objectives
Define terms
n Define a database using SQL data definition
language
n Write single table queries using SQL
n Establish referential integrity using SQL
n Discuss SQL:1999 and SQL:200n standards
n
Figure 6-1
A simplified schematic of a typical SQL environment, as
described by the SQL: 200n standard
SQL Environment
n
6. Identify constraints on
columns (domain
specifications)
Figure 6-6 SQL database definition commands for Pine Valley Furniture
Company (Oracle 11g)
Overall table
definitions
2. Non-nullable specification
Primary keys
can never have
NULL values
Non-nullable specifications
Primary key
5. Default value
6. Domain constraint
Primary key of
parent table
STUDENT'(StudentID,'StudentName)'
'
'
QUALIFIED'(FacultyID,'CourseID,'DateQualified)'
'
StudentID)
StudentName'
FacultyID)
CourseID)
DateQualified'
38214'
54907'
66324'
70542'
'
Letersky'
Altvater'
Aiken'
Marra'
'
2143'
2143'
3467'
3467'
4756'
4756'
'
ISM'3112'
ISM'3113'
ISM'4212'
ISM'4930'
ISM'3113'
ISM'3112'
'
9/1988'
9/1988'
9/1995'
9/1996'
9/1991'
9/1991'
'
FACULTY'(FacultyID,'FacultyName)'
'
'
SECTION'(SectionNo,'Semester,'CourseID)'
'
FacultyID)
FacultyName'
SectionNo)
Semester)
CourseID'
2143'
3467'
4756'
'
Birkin'
Berndt'
Collins'
'
2712'
2713'
2714'
2715'
'
IR2008'
IR2008'
IR2008'
IR2008'
'
ISM'3113'
ISM'3113'
ISM'4212'
ISM'4930'
'
COURSE'(CourseID,'CourseName)'
'
'
REGISTRATION'(StudentID,'SectionNo,'Semester)'
'
CourseID)
CourseName'
StudentID)
SectionNo)
Semester)
ISM'3113'
ISM'3112'
ISM'4212'
ISM'4930'
'
Syst'Analysis'
Syst'Design'
Database'
Networking'
'
38214'
54907'
54907'
66324'
RRR'
2714'
2714'
2715'
2713'
IR2008'
IR2008'
IR2008'
IR2008'
'
'
Practice: Exercise #1
Write a database description for each of the relations shown,
using SQL DDL. Assume the following attribute data types:
StudentID (integer, primary key)
StudentName (max 25 characters)
FacultyID (integer, primary key)
to
FacultyName (max 25 characters)
e in
d
o
c
l
CourseID (8 characters, primary key)
Q L eg.sq
S
r
tR
ou
CourseName (max 15 characters)
e y uden
v
a
S le St
DateQualified (date)
a fi
SectionNo (integer, primary key)
Semester (max 7 characters)
Using MySQL
n
n
n
n
n
n
n
Available on csdb.csc.villanova.edu
Invoke with
mysql u username D database -p
SHOW DATABASES;
SHOW TABLES;
DESCRIBE name_T; (or SHOW COLUMNS FROM name_T;)
SOURCE script.sql
\! shell_command
Conventions
n
n
n
n
n
Changing Tables
n
Table Actions:
Removing Tables
n
Practice: Exercise #4
Write SQL data definition commands for each of the following:
1. Add an attribute Class to the Student table, then drop it
2. Create a new Dummy table, then remove it
3. Change the FacultyName field from 25 characters to 40
characters
Insert
n
Insert Statement
n
STUDENT'(StudentID,'StudentName)'
'
'
StudentID)
StudentName'
38214'
54907'
66324'
70542'
'
Letersky'
Altvater'
Aiken'
Marra'
'
QUALIFIED'(FacultyID,'CourseID,'DateQualified)'
'
FacultyID)
'
ISM'3112'
ISM'3113'
ISM'4212'
ISM'4930'
ISM'3113'
ISM'3112'
'
9/1988'
9/1988'
9/1995'
9/1996'
9/1991'
9/1991'
'
FacultyID)
FacultyName'
SectionNo)
Semester)
CourseID'
2143'
3467'
4756'
'
Birkin'
Berndt'
Collins'
'
2712'
2713'
2714'
2715'
'
IR2008'
IR2008'
IR2008'
IR2008'
'
ISM'3113'
ISM'3113'
ISM'4212'
ISM'4930'
'
COURSE'(CourseID,'CourseName)'
'
'
DateQualified'
l
sq
g. ith
e
R sw
nt
de table
u
St our
end late y
t
x
E opu
to p data.
this
SECTION'(SectionNo,'Semester,'CourseID)'
'
FACULTY'(FacultyID,'FacultyName)'
'
CourseID)
2143'
2143'
3467'
3467'
4756'
4756'
'
REGISTRATION'(StudentID,'SectionNo,'Semester)'
'
CourseID)
CourseName'
StudentID)
SectionNo)
Semester)
ISM'3113'
ISM'3112'
ISM'4212'
ISM'4930'
'
Syst'Analysis'
Syst'Design'
Database'
Networking'
'
38214'
54907'
54907'
66324'
RRR'
2714'
2714'
2715'
2713'
IR2008'
IR2008'
IR2008'
IR2008'
'
'
Delete Statement
n
Delete Statement
n
Update Statement
n
table
SET field = value
WHERE conditions
Update Statement
n
Do this:
-Add an Age field to the Student_T table, with a
default value of 18
-Increment the Age of the student with ID 54907
Practice: Exercise #5
Write SQL commands for the following:
1. Create two different forms of the INSERT command to add a
student with a student ID of 65798 and last name Lopez to
the Student table.
2. Now write a command that will remove Lopez from the
Student table.
3. Create an SQL command that will modify the name of course
ISM 4212 from Database to Introduction to Relational
Databases.
Restricting:
Deletes of primary records
n Updates of primary records
n Inserts of dependent records
n
STUDENT'(StudentID,'StudentName)'
'
'
QUALIFIED'(FacultyID,'CourseID,'DateQualified)'
'
StudentID)
StudentName'
FacultyID)
CourseID)
DateQualified'
38214'
54907'
66324'
70542'
'
Letersky'
Altvater'
Aiken'
Marra'
'
2143'
2143'
3467'
3467'
4756'
4756'
'
ISM'3112'
ISM'3113'
ISM'4212'
ISM'4930'
ISM'3113'
ISM'3112'
'
9/1988'
9/1988'
9/1995'
9/1996'
9/1991'
9/1991'
'
FACULTY'(FacultyID,'FacultyName)'
'
'
a
Dat
FacultyID)
FacultyName'
SectionNo)
Semester)
CourseID'
2143'
3467'
4756'
'
Birkin'
Berndt'
Collins'
'
2712'
2713'
2714'
2715'
'
IR2008'
IR2008'
IR2008'
IR2008'
'
ISM'3113'
ISM'3113'
ISM'4212'
ISM'4930'
COURSE'(CourseID,'CourseName)'
'
s
trol
SECTION'(SectionNo,'Semester,'CourseID)'
'
'
'
do not allow */
propagate change */
Set MajorId to NULL */
Set MajorId to its default value */
REGISTRATION'(StudentID,'SectionNo,'Semester)'
'
CourseID)
CourseName'
StudentID)
SectionNo)
Semester)
ISM'3113'
ISM'3112'
ISM'4212'
ISM'4930'
'
Syst'Analysis'
Syst'Design'
Database'
Networking'
'
38214'
54907'
54907'
66324'
RRR'
2714'
2714'
2715'
2713'
IR2008'
IR2008'
IR2008'
IR2008'
'
'
on
yC
t
i
r
g
Inte
Basic SELECT
Basic SELECT
n
Basic SELECT
n
SELECT Example
n
Practice: Exercise #6
Write SQL queries to answer the following questions:
1. Which students have an ID number that is less than 50000?
2. What is the name of the faculty member whose ID is 4756?
Summary
n
DDL
- CREATE TABLE
- DROP TABLE
- ALTER TABLE
DML
- INSERT INTO
- UPDATE
- DELETE FROM
- SELECT