0% found this document useful (0 votes)
78 views5 pages

SQL Tables, Keys, Views, Indexes: CS145 Lecture Notes #8

This document summarizes key SQL concepts related to database tables, keys, views, and indexes. It discusses: 1) How to create and drop tables using SQL syntax, specifying data types for columns. Primary and unique keys can be declared to enforce data integrity. 2) Views are defined by a query and stored as definitions rather than data. They can be used in queries like tables. Views allow hiding data and making queries easier. 3) Indexes on columns can dramatically speed up queries that filter or sort on those columns. Indexes are created using SQL syntax and can improve performance of joins and other operations. Choosing appropriate indexes requires considering expected queries and table sizes.

Uploaded by

Nagesh Khandare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views5 pages

SQL Tables, Keys, Views, Indexes: CS145 Lecture Notes #8

This document summarizes key SQL concepts related to database tables, keys, views, and indexes. It discusses: 1) How to create and drop tables using SQL syntax, specifying data types for columns. Primary and unique keys can be declared to enforce data integrity. 2) Views are defined by a query and stored as definitions rather than data. They can be used in queries like tables. Views allow hiding data and making queries easier. 3) Indexes on columns can dramatically speed up queries that filter or sort on those columns. Indexes are created using SQL syntax and can improve performance of joins and other operations. Choosing appropriate indexes requires considering expected queries and table sizes.

Uploaded by

Nagesh Khandare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CS145 Lecture Notes #8

SQL Tables, Keys, Views, Indexes


Creating & Dropping Tables
Basic syntax:
CREATE TABLE ( DROP TABLE ;
,
, ...,
);
Types available:
INT or INTEGER
REAL or FLOAT
CHAR( ), VARCHAR( )
DATE, TIME
Example:
CREATE TABLE Student (SID INTEGER,
name CHAR(30),
age INTEGER,
GPA FLOAT);
CREATE TABLE Take (SID INTEGER,
CID CHAR(10));
CREATE TABLE Course (CID CHAR(10),
title VARCHAR(100));
Keys
Recall: a set of attributes is a key for a relation if
(1) In no instance of will two different tuples agree on all attributes of
; i.e., is a tuple identier
(2) No proper subset of satises (1); i.e., is minimal
Declaring Keys
SQL allows multiple keys to be declared for one table:
At most one PRIMARY KEY per table
Any number of UNIQUE keys per table
Jun Yang 1 CS145 Spring 1999
Two places to declare keys in CREATE TABLE:
After an attributes type, if the attribute is a key by itself
As a separate element (essential if key has more than one attribute)
Example:
CREATE TABLE Student (SID INTEGER PRIMARY KEY,
name CHAR(30),
age INTEGER,
GPA FLOAT);
CREATE TABLE Take (SID INTEGER,
CID CHAR(10),
PRIMARY KEY(SID, CID));
CREATE TABLE Course (CID CHAR(10) PRIMARY KEY,
title VARCHAR(100) UNIQUE);
Why declare keys?
They are integrity constraints enforced by the DBMS
They tell the DBMS to expect frequent lookups using key values
Keys vs. FDs in SQL
Recall in the pure relational model (where every relation is duplicate-free):
is a tuple identier for
is a tuple identier for
In SQL (where a table may contain duplicate tuples):
If is a tuple identier for , then must be duplicate-free
and may still hold when contains
duplicates
is a tuple identier or
Example:
Views
A view is like a virtual table:
It is dened by a view denition query which describes how to com-
pute the view contents
DBMS stores the view denition instead of the view contents
It can be used in queries just like a regular table
Jun Yang 2 CS145 Spring 1999
Creating & Dropping Views
Syntax:
CREATE VIEW AS ;
DROP VIEW ;
Example: StudentRoster view
Example: CS145Roster view
Using Views in Queries
Semantics:
SELECT...FROM..., ,...WHERE...;
SELECT...FROM...,( ) ,...WHERE...;
Example: nd the SID of a CS145 student named Bart
No more joins!
DBMS typically rewrites the query to make it more efcient to evaluate
Why use views?
To hide some data from the users
To make certain queries easier or more natural to express
Real database applications use tons and tons of views
Modifying Views
Does not seem to make sense since views are virtual
But does make sense if that is how user views the database
Modify the base tables such that the modication would appear to
have been accomplished on the view
Example: DELETE FROM StudentRoster WHERE SID = 123;
Jun Yang 3 CS145 Spring 1999
Sometimes it is not possible
Example:
CREATE VIEW HighGPAStudent AS
SELECT * FROM Student WHERE GPA > 3.7;
INSERT INTO HighGPAStudent
VALUES(888, Nelson, 10, 2.50);
Sometimes there are too many possibilities
Example:
CREATE VIEW AvgGPA AS
SELECT AVG(GPA) AS GPA FROM Student;
UPDATE AvgGPA
SET GPA = 2.5;
Precise conditions for modiable views are very complicated
SQL2 uses conservative conditions: views must be dened as single-
table SELECT with simple WHERE, no aggregates, no subqueries, etc.
Indexes
An index on attribute
Creates auxiliary persistent data structure
Can dramatically speed up accesses of the form:
(sometimes; depending the type of index)
An index can be built on a combination of multiple attributes as well
Data structures for indexes: sorted lookup tables, hash tables, search
trees, etc. (CS245 and CS346)
Example:
SELECT * FROM Student WHERE name = Bart;
Without index on Student.name: because we store tables as at col-
lections of unordered tuples, we must scan all Student tuples
With index: go directly to tuples with name = Bart
Example:
SELECT * FROM Student, Take
WHERE Student.SID = Take.SID;
Use index on either Student.SID or Take.SID to speed up join
Jun Yang 4 CS145 Spring 1999
Creating & Dropping Indexes
Syntax:
CREATE INDEX ON ( , ..., );
DROP INDEX ;
If CREATE is followed by UNIQUE, DBMS will also enforce that
is a key of
Choosing which indexes to create is a difcult design issue:
Depends on the expected query/update load and size of tables
Jun Yang 5 CS145 Spring 1999

You might also like