Chapter 3: SQL Flashcards
What is a Data Definition Language (DDL)?
A language that operates on the structure of the database
- schema for relations
- domain values with attributes
- integrity constraints
- set of indices for each relation
- security and authorization info for relations
- physical storage structure on the disk
What are some domain types in SQL
char(n) - fixed length character string with user specified length n
varchar(n) - variable length character strings wit user specified maximum length n
int - integer
smallint - small integer
numeric(p,d) - fixed point number with user specified precision of p digits, with n digits to the right of decimal point
real, double precision - floating point and double-precision floating numbers, machine dependent precision
float(n) - floating point number with user specified precision of at least n digits
What does the CREATE TABLE command do?
creates a table as follows
CREATE TABLE r (A1 D1, …, AnDn,
(integrity-constraint 1), …
(integrity-constraint k)
)
What are some integrity constraints in CREATE TABLE
not null
-a thing isn’t null
primary key (A1, …, An)
-declaration on an attribute automatically ensures not null (SQL-92 and onwards, needs to be explicitly declared in SQL-89)
What does the DROP TABLE command do?
deletes all information about the dropped relation from the database
What does the ALTER TABLE command do?
adds/drops attributes to an existing relation
- ALTER TABLE r ADD a d
- a is name of attribute being added to relation r and d is domain of a
- ALTER TABLE r DROP a
- a is the name of an attribute of relation r
What is the basic structure of an SQL query?
3) SELECT A1,A2,…,An
1) FROM r1,r2, …, rn
2) WHERE P
* *where is optional**
How do you force the elimination of duplicates/multiset in your query result?
SELECT DISTINCT instead of SELECT
- SQL allows multiset, and normal SELECT can return multiset
- select clause can also contain arithmetic
What is the purpose of the WHERE clause?
specifies conditions that the result must satisfy
-corresponds to selection predicate in relational algebra
How do you combine comparison results in the WHERE clause?
Use logical connectives AND, OR, and NOT
How does the BETWEEN comparison operator work?
WHERE thing between (lower bound) and (upper bound)
-is inclusive, greater than or equal to and less than or equal to
What is the purpose of the SELECT clause?
lists the attributes in the result of a query
-corresponds to the projection operation in relational algebra
What is the purpose of the FROM clause?
lists the relations involved in the query
-corresponds to the cartesian product operation of the relational algebra
What is the purpose of the rename operation and how is it done in SQL?
it allows for the renaming of relations and attributes within a query
-in SQL is the AS clause
Where are temp variables defined?
They are defined in the from clause by use of the as clause
-makes temp variables to be referenced for the query