Documente Academic
Documente Profesional
Documente Cultură
Each implementation of SQL uses slightly different names for the data types.
2. Character Strings
1. TIME (i): Defines the TIME data type with an additional i positions for fractions of a
second. For example: HH:MM:SS:dd and Offset from UTZ. +/- HH:MM
2. TIMESTAMP
3. INTERVAL: Used to specify some span of time measured in days or minutes, etc.
Note: MS Access, SQL Server and Oracle store date and time information together in a
DATE data type.
Storage
Data Type Range of Values
Size
Byte 1 byte 0 to 255
Boolean 2 bytes True or False.
Integer 2 bytes -32,768 to 32,767.
Long (long
4 bytes -2,147,483,648 to 2,147,483,647.
integer)
Single (single- -3.402823E38 to -1.401298E-45 for negative
precision 4 bytes values; 1.401298E-45 to 3.402823E38 for
floating-point) positive values.
-1.79769313486232E308 to
Double (double-
-4.94065645841247E-324 for negative values;
precision 8 bytes
4.94065645841247E-324 to
floating-point)
1.79769313486232E308 for positive values.
Currency (scaled -922,337,203,685,477.5808 to
8 bytes
integer) 922,337,203,685,477.5807.
Date 8 bytes January 1, 100 to December 31, 9999.
Object 4 bytes Any Object reference.
10 bytes +
String (variable- 0 to approx. 2 billion (approx. 65,400 for MS
string
length) Windows version 3.1).
length
String (fixed- Length of
1 to approximately 65,400.
length) string
Variant (with
16 bytes Any numeric value up to the range of a Double.
numbers)
22 bytes +
Variant (with
string Same range as for variable-length String.
characters)
length
NOTE: SQL2 specification supports the creation of multiple schemas per database each
with a distinct owner and authorized users.
2. Creating a Schema
Note: To try out these SQL examples in MS Access, go to the Queries form and choose New,
then choose Design View and then close the next dialog box. Under the View menu, choose
SQL. From this point, you can type in any SQL statement and execute it. Note that MS
Access's DDL syntax is extremely limited. Most of the DDL statements below (including
domains, NOT NULL constraints and referential integrity constraints) are not supported.
3. Creating a Table:
Note: When naming tables, columns and other database objects, do not include spaces in the
names. For example, do not call the last name column: Last Name
If you wish to separate words in a name, use the underscore character.
Constraints on attributes:
I. Immediate
II. Deferrable until commit time
6. Referential Integrity Constraint: Specify the behavior for child tuples when a parent
tuple is modified.
Constraints can also be given names so that they can later be modified or dropped
easily.
CONSTRAINT pk_order_items
PRIMARY KEY (order_number, line_item),
CONSTRAINT fk1_order_items
FORIEGN KEY (order_number)
REFERENCES order_header (order_number)
ON DELETE SET DEFAULT
ON UPDATE CASCADE,
CONSTRAINT fk2_order_items
FOREIGN KEY (part_number)
REFERENCES parts (part_number)
ON DELETE SET DEFAULT
ON UPDATE CASCADE
);
An even better approach is to create the tables without constraints and then add them
separately with ALTER TABLE statements
We give the first part of the index name as "idx" just as a convention.
Changing Attributes:
ALTER TABLE student ALTER last_name VARCHAR(35);
ALTER TABLE student ALTER gpa DROP DEFAULT
ALTER TABLE student ALTER gpa SET DEFAULT 0.00;
Adding Attributes:
ALTER TABLE student ADD admission DATE;
DDL is used to create and specify the schema. DML is then used to manipulate (select,
insert, update, delete) data.
General syntax:
Examples:
Main way of getting data out of tables is with the SELECT statement.
SELECT syntax:
SELECT *
FROM employees
ORDER BY 2;
SELECT AVG(salary)
FROM employee
WHERE state = 'NJ'
SELECT *
FROM employee
WHERE last_name = 'Smith' AND state = 'NY'
SELECT AVG(grade)
FROM students;
Results:
AVG (GRADE)
----------
89.1428571
Give the name of the student with the highest grade in the class:
Results:
NAME GRADE
-------------- -----
Mary 98
Results:
Note the two aliases given to the students table: s1 and s2. These allow us to refer
to different views of the same table.
SELECTING FROM 2 OR MORE TABLES
In the FROM portion, list all tables separated by commas. Called a Join.
SELECT employee.name
FROM employee, department
WHERE employee. department = department. department
AND department. location = 'CA';
Results:
NAME
--------------------------------
Jill
Jack
Fred
List each employee name and what state (location) they work in. List them in order
of location and name:
List each department and all employees that work there. Show the department and
location even if no employees work there.
Results:
DEPARTMENT LOCATION NAME
------------- ---------------- ----------------
Accounting CA Fred
MIS CA Jack
MIS CA Jill
Finance NJ Alice
Finance NJ Joe
Marketing NY NULL
SELECT MAX(employee.salary)
FROM employee, department
WHERE employee.department = department.department
AND department.location = 'CA';
Results:
MAX(SALARY)
------------
48000
Results:
LASTNAME SUM(BALANCE)
--------- ------------
Axe $15,000.00
Builder $1,300.00
Jones $1,000.00
Smith $6,000.00
We can also use a Column Alias to change the title of the columns
Results:
LASTNAME TotalBalance
--------- ------------
Axe $15,000.00
Builder $1,300.00
Jones $1,000.00
Smith $6,000.00
Results:
For example: A student can tutor one or more other students. A student has only one tutor.
STUDENTS (StudentID, Name, Student_TutorID)
Results:
Student Tutor
---------- ----------
Alex Bill
Mary Bill
Sue Bill
Liz Mary
Ed Mary
Petra Sue
The above is called a "recursive" query because it access the same table two times.
We give the table two aliases called s1 and tutors so that we can compare different aspects of
the same table.
However, as is, the table is missing something: We don't see who is tutoring Bill Smith. Use
LEFT JOIN:
Results:
Student Tutor
---------- ----------
Bill
Alex Bill
Mary Bill
Sue Bill
Liz Mary
Ed Mary
Petra Sue
Here is one more twist: Suppose we were interested in those students who do not tutor
anyone? Use RIGHT JOIN
Results:
TutorName NumberTutored
---------- -------------
Bill 3
Mary 2
Sue 1
SELECT name
FROM employee
WHERE department IN ('Finance', 'MIS');
SELECT name
FROM employee
WHERE department IN
(SELECT department
FROM departments
WHERE location = 'CA');
In the above case, the subquery returns a set of tuples. The IN clause returns
true when a tuple matches a member of the set.
Results:
name salary
----------- ----------
Joe 50000
Jill 48000
Fred 33000
The above query shows all employees names and salaries where there is at least
one person who makes more money (the first exists) and at least one person who
makes less money (second exists).
vii) NOT EXISTS:
Results:
name salary
--------- ----------
Alice 52000
Above query shows all employees for whom there does not exist an employee
who is paid less.
Use the LIKE operator to perform a partial string match. Generally, the %
character is used as the wild card although in some DBMS, the * character is
used.
Show all employees whose name contains the letter 'e' and the letter 'n' in that
order:
With no WHERE clause, DELETE will remove all tuples from a table.
DELETE employee;
DELETE employee
WHERE salary > 50000;
DELETE employee
WHERE department IN
(SELECT department
FROM department
WHERE location = 'CA');
DELETE will not be successful if a constraint would be violated. For example, consider
the department attribute in the Employee table as a Foreign Key. Removing a
department would then be contingent upon no employees working in that department.
This is what we call enforcing Referential Integrity
UPDATE employee
SET last_name = 'Smith'
WHERE employee_id = 'E1001';
UPDATE employee
SET salary = salary * 1.05
WHERE employee_id = 'E1001';
8. DEFINING VIEWS:
It is possible to define a particular view of a table (or tables). For example, if we commonly
access just 2 or 3 columns in a table, we can define a view on that table and then use the view
name when specifying queries.
SELECT *
FROM emp_address
ORDER BY last_name;
SELECT *
FROM avg_sal_dept
WHERE department = 'Finance';