Chapter 3: SQL Flashcards

1
Q

What is a Data Definition Language (DDL)?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are some domain types in SQL

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the CREATE TABLE command do?

A

creates a table as follows
CREATE TABLE r (A1 D1, …, AnDn,
(integrity-constraint 1), …
(integrity-constraint k)
)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are some integrity constraints in CREATE TABLE

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the DROP TABLE command do?

A

deletes all information about the dropped relation from the database

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the ALTER TABLE command do?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the basic structure of an SQL query?

A

3) SELECT A1,A2,…,An
1) FROM r1,r2, …, rn
2) WHERE P
* *where is optional**

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you force the elimination of duplicates/multiset in your query result?

A

SELECT DISTINCT instead of SELECT

  • SQL allows multiset, and normal SELECT can return multiset
  • select clause can also contain arithmetic
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the purpose of the WHERE clause?

A

specifies conditions that the result must satisfy

-corresponds to selection predicate in relational algebra

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you combine comparison results in the WHERE clause?

A

Use logical connectives AND, OR, and NOT

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does the BETWEEN comparison operator work?

A

WHERE thing between (lower bound) and (upper bound)

-is inclusive, greater than or equal to and less than or equal to

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the purpose of the SELECT clause?

A

lists the attributes in the result of a query

-corresponds to the projection operation in relational algebra

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose of the FROM clause?

A

lists the relations involved in the query

-corresponds to the cartesian product operation of the relational algebra

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the purpose of the rename operation and how is it done in SQL?

A

it allows for the renaming of relations and attributes within a query
-in SQL is the AS clause

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Where are temp variables defined?

A

They are defined in the from clause by use of the as clause

-makes temp variables to be referenced for the query

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the two string operator special characters?

A
'%'
-matches any substring
'_'
-matches any character
**used after keyword LIKE
**to get actual _ and % need \ before, \% and \_
17
Q

What are the set operators in SQL?

A

UNION, INTERSECT, and EXCEPT

-to make them multi-set use keyword ALL

18
Q

What does the having clause do?

A

Acts as a filter for the temporary output generated by the group by clause,
allows you to selectively output results from group by

19
Q

What does a SQL query with a having clause look like?

A
SELECT z, ag(a)
FROM    T1,..., Tn
WHERE  theta
GROUP BY z
HAVING  ag1(a)
20
Q

How can you check for null values in SQL?

A

use IS NULL

21
Q

What are Nested Subqueries

A

They are queries within a query

  • they are a matter of preference
  • any NSQ can be written as a set operation or a join, and vice-versa
  • they run at O(n^2) because there is an outer and inner loop
22
Q

What does the SOME clause do?

A

checks is there is at least one thing that meets the desired situation
- (=some) = in

23
Q

How do you test for empty relations?

A

Use EXISTS and NOT EXISTS

  • exists returns true if the subquery has an output
  • not exists returns true if the subquery has no output
24
Q

What does UNIQUE do?

A

tests if a subquery has any duplicate tuples in its result

25
Q

What is a correlation variable?

A

Variable from outer query that is utilized in a NSQ, this is done by referencing the outer query variable within the NSQ

26
Q

What is a derived relation?

A

A relation produced by a NSQ within the from clause, the NSQ produces a relation which then becomes the relation utilized in the outer query

27
Q

What is the purpose of the WITH clause?

A

It provides a way of defining temporary tables, allowing for more complex queries

28
Q

How does referencing temporary tables made using the WITH clause work?

A

The temp tables made, and any commands within their queries, only have access to the tables created before the execution of the current line. (can’t reference tables made after current table only before)

29
Q

What is a VIEW

A

A VIEW is a QUERY WITH A NAME otherwise called a PRESERVED QUERY.
-query is stored in a system table tied to the view name so when view is called the query tied to that view is ran.

30
Q

How long does it take to create a view?

A

Since creating a view is the act of storing the view’s query in the system table, (VIEW_NAME, QUERY), it is very fast (takes no time). Utilizing and calling the view can take time though.

31
Q

How do VIEWS and WITH statements differ?

A

VIEW
-makes a temporary table that is permanent and can be accessed outside of current query
-is public
WITH
-makes temporary table that is temporary, can only be accessed within the query
-is private

32
Q

How does INSERT INTO (insertion) function?

A
  • inserts new rows/tuples in table
  • works row wise
  • INSERT INTO table VALUES(col 1, col 2, …)
  • any values that you don’t want to insert can specify as null
33
Q

How does UPDATE (updates) function?

A
  • updates values in tables
  • works column wise
  • UPDATE table SET column value stat WHERE more stuff
34
Q

How can the CASE statement be used for conditional updates?

A

can use CASE stuff END in the set portion a an UPDATE to make if then else.
- UPDATE table SET attribute = CASE WHEN attribute condition THEN stuff ELSE stuff END

35
Q

What can be done with VIEWS and is recommended against and why?

A

You can do updates, inserts and deletes in a view but it can get very complicated so it is not recommended.