SQL Flashcards

1
Q

SQL Definition

A

High Level, declarative programming language

Specifies what the user wants, not how

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

Terms:

Table is a:
Row is a:
Column is a:

A

Table is a: relation
Row is a: tuple
Column is an attribute:

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

Table Creation in SQL:

A

CREATE TABLE STUDENT(
Fname VARCHAR(15) NOT NULL,
SSN INT NOT NULL);

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

Domains:

A

A constant

CREATE DOMAIN SSN_TYPE AS CHAR(9);

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

Primary Key Declartion

A

CREATE TABLE STUDENT(
Fname VARCHAR(15) NOT NULL,
SSN INT NOT NULL,
PRIMARY KEY(SSN));

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

Unique Key Declaration

A

Provides alternative keys

CREATE TABLE STUDENT(
    Fname VARCHAR(15) NOT NULL,
    SSN INT NOT NULL,
    SID INT UNIQUE
    PRIMARY KEY(SSN));
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Default declaration

A

For when a value isn’t specified:

Dno INT NOT NULL DEFAULT 3;

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

SQL Insert

A

INSERT INTO EMP VALUES(
“Bob”, “Jones”, 9995478414);

For specific attributes:

INSERT INTO EMP(Fname, Lname) VALUES(
“Bob”, “Jones”);

Anything not set is NULL

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

SQL Delete

A

DELETE FROM EMP
WHERE Fname = “Bob” AND Lname = “Smith”;

Delete entire table

DELETE FROM EMP

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

SQL Update

A

UPDATE EMP
SET Lname = “Steppers
WHERE Ssn = 4784514784;

Update more than one person

UPDATE EMP
SET Sal = Sal * 1.1
WHERE Dno = 5

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

SQL Join

A

SELECT Fname, Lname
FROM EMP, DEPT
WHERE Dname = “Research” AND Ename = “John”

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

Attribute Wildcard

A

Can use * to denote all attributes from a selected entity
SELECT *
FROM EMP

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

SQL UNION, INTERSECT, EXCEPT

A

(SQL STATEMENT)
UNION
(SQL STATEMENT)

Same goes for the intersection and except

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

Where Wildcard

A

For zero or more characters

Select Lname
FROM EMP
WHERE Lname LIKE ‘sellers.%’

For exactly one character

SELECT Lname
FROM EMP
WHERE Lname LIKE ‘sellers.18_’

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

Arithmetic Ops

A

SELECT 1.1 * SALARY AS INCREASE

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

Ranges

A

Can use <= or >= or BETWEEN keyword

SELECT *
FROM EMP
WHERE SAL >= 40000 AND SAL <= 60000

SELECT *
FROM EMP
WHERE SAL BETWEEN 40000 AND 60000

17
Q

Order by:

A

Forces the results to come back in a specific order

SELECT *
FROM EMP
ORDER BY Lname DESC, S_ssn

Would order first by the last name in descending order, if there was a tie, then order by the super’s ssn.

18
Q

Views:

A

A virtual table, changes to a view table reflect back to the actual tables they are derived from

CREATE VIEW V1
AS SELECT Fname, Lname, Dept_name
FROM EMP, DEPT
WHERE Ssn = Essn

19
Q

Compact OR’s

A

SELECT Lname
FROM EMP
WHERE Dno IN (25,47,58)

Gets all the last names where the Dno is 25 or 47 or 58

20
Q

Nested queries

A
SELECT Essn
FROM EMP
WHERE (Pno, Hours) IN
    (SELECT Pno, Hours
     FROM EMP
     WHERE Essn = '123456789');

Finds all employee SSN who work on the same project and same hours as employee 123456789