SQL: Structured Query Language
SQL: Structured Query Language
LANGUAGE
QUERIES
Relational Algebra (formal mathematical
OUR EXAMPLE
DATABASE
student ( stuid, stuname, major, credits )
faculty ( facid, facname, dept, rank )
class ( course#, facid, sched, room )
enroll ( course#, stuid, grade )
FUNDAMENTAL SQL
VERBS FOR ACTIVITIES
to define the structure, we
DROP TABLE)
to query and manipulate the information in the
database, we
SELECT
UPDATE
INSERT
DELETE
CREATE VIEW
CREATE A TABLE
CREATE TABLE student
(stuid
CHAR(9) NOT NULL,
stuname CHAR(20) NOT NULL,
major
CHAR(20),
credits
SMALLINT,
PRIMARY KEY (stuid));
DECIMAL (p,q)
A decimal number p digits long, with q of these being decimal places; so,
DECIMAL (5,2) represents a number with three digits to the left of the
decimal, and two digits to the right of the decimal point.
CHAR (n)
Character string n characters long
DATE
Dates in the form DD-Month-YY or MM/DD/YYYY ; thus, February 16,
2010 could be represented as 16-February-2010 or 2/16/2010
algebra,
but, unfortunately, is not at all like the select
operator in the relational algebra
and, also, unfortunately, does not eliminate duplicates.
------------------------------------------------------------------SELECT stuname, stuid, credits
FROM student
WHERE major = IT';
SELECT
FROM
course#
enroll;
SELECT
FROM
*
student;
SELECT stuname
FROM
student
WHERE major = IT' AND credits > 60;
null values
each SQL function returns a single value, defined as follows:
COUNT returns the number of values in a column
SUM returns the sum of values in a column
AVG returns the average of the values in the column
MAX returns the maximum of the values in the column
MIN returns the minimum of the values in the column
COUNT (DISTINCT column-name) eliminates duplicate
values in the column
COUNT ( * ) counts the total number of rows including nulls
& duplicates
SELECT
FROM
WHERE
stuid, stuname
student
credits =
( SELECT MAX (credits)
FROM
student);
SELECT
FROM
GROUP BY
HAVING
course#
enroll
course#
COUNT ( * ) < 3;
UPDATE
SET
WHERE
student
major = IT'
stuid = '123456789';
UPDATE
SET
faculty
dept = IS'
rank = 'assistant'
WHERE
facname = 'annber';
INSERT
INTO
faculty
VALUES ('987654321', 'jones', 'che', 'instructor');
DELETE
FROM
enroll
WHERE stuid = '135792468';