SQL

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/106

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

107 Terms

1
New cards

SQL

Structured Query Language; standard language for relational databases.

2
New cards

SEQUEL (historical name)

Original name for SQL

3
New cards

Core SQL

The common, standard part used in basically all relational DB setups.
ex. CREATE TABLE, SELECT, INSERT, UPDATE, DELETE.

4
New cards

SQL extensions

Vendor-specific additions beyond the core.
Different DBs support different extensions.
ex. Specialized analytics/data mining features.

5
New cards

Statement terminator (;)

Ends an SQL statement.
Without it, DB may wait for more input.

6
New cards

DDL (Data Definition Language)

SQL statements that define/change schema objects.
Defines tables/domains; removes objects.
ex. CREATE TABLE, CREATE DOMAIN, ALTER TABLE, DROP TABLE.

7
New cards

DML (Data Manipulation Language)

SQL statements that add/change/delete table rows.
Used for actual data.
ex. INSERT, UPDATE, DELETE.

8
New cards

Query

SQL that asks the DB for data.
Usually SELECT.
ex. SELECT Name FROM PERSON WHERE …

9
New cards

CREATE TABLE

Creates a new table (relation) with attributes, types, and constraints.

10
New cards

Attribute / column

A named field in a table with a data type (and maybe constraints).
Defines what kind of values a column can store.
ex. Name VARCHAR(30) NOT NULL.

11
New cards

Row / tuple

One record in a table.
A single collection of column values.

12
New cards

Data type

Defines what kind of data a column can hold.
Prevents invalid values and shapes storage.
ex. INT vs VARCHAR vs DATE.

13
New cards

INT (ex. Id INT)

Integer number type.
Used for counts/IDs.

14
New cards

SMALLINT (ex. Credit_hours SMALLINT)

Smaller-range integer type.
Can save space vs INT.

15
New cards

FLOAT (ex. GPA FLOAT)

Approximate numeric type.
Used for decimals where exactness isn’t critical.

16
New cards

DOUBLE PRECISION

More precise floating-point type than FLOAT.

17
New cards

CHAR(n)

ex. (CHAR(12) phone format like zzz-zzz-zzzz)

Fixed-length character string, padded to n characters.
Good when length is always the same.

18
New cards

VARCHAR(n)

ex. (Name VARCHAR(30))

Variable-length string up to n characters.
Saves space when lengths vary.

19
New cards

BOOLEAN

TRUE or FALSE.
NULL means unknown (neither true nor false).

20
New cards

DATE

Calendar date.
Format shown: YYYY-MM-DD.

21
New cards

TIME

Time of day.
Format shown: HH:MM:SS.

22
New cards

TIMESTAMP

Date + time together.
Used for event times.

23
New cards

Domain (custom type)

(ex. CREATE DOMAIN NET_ID AS CHAR(9).)

A named type you define, often as an alias.
Lets you change the underlying type in one place.

24
New cards

CREATE DOMAIN

Creates a named domain (custom data type).
Used to standardize types across tables.
ex. NET_ID instead of repeating CHAR(9).

25
New cards

Constraint

A rule the data must satisfy; DB rejects inserts/updates that violate it.
Used to keep data valid.
ex. NOT NULL, PRIMARY KEY, FOREIGN KEY, CHECK.

26
New cards

NOT NULL

ex. Email VARCHAR(40) NOT NULL.

Column cannot be NULL.
Forces a value to be provided.

27
New cards

PRIMARY KEY

ex. Netid PRIMARY KEY.

Uniquely identifies each row.
Cannot be NULL; enforces uniqueness.

28
New cards

Composite primary key

Primary key made of multiple columns.
Declared as PRIMARY KEY(col1, col2).

ex. PRIMARY KEY(Studentid, Courseid).

29
New cards

Unique identification

ex. No two PERSON rows share the same Netid.

Idea behind keys: one row per key value.
Makes rows referable and joinable.

30
New cards

FOREIGN KEY

ex. SECTION.Instructor_id REFERENCES PERSON(Netid).

Column(s) that must match a key in another table.
Links tables and prevents orphan references.

31
New cards

REFERENCES

ex. REFERENCES PERSON(Netid).

SQL keyword used in foreign key definitions.
States the parent table and referenced column(s).

32
New cards

Parent table

ex. PERSON is parent of SECTION (Instructor_id).

The referenced table in a foreign key relationship.
Holds the “original” key values.

33
New cards

Child table

The table that contains the foreign key.
Depends on parent table values.
ex. SECTION depends on PERSON(Netid).

34
New cards

Orphan row

A row with a foreign key value that doesn’t exist in the parent.
FK constraints prevent this.
ex. SECTION.Instructor_id = 'x' with no PERSON('x').

35
New cards

Referential action

What the DB does to child rows when a referenced parent row is deleted/updated.
Declared on the foreign key side.
ex. ON DELETE …, ON UPDATE ….

36
New cards

ON DELETE CASCADE

ex. Delete PERSON → delete their SECTION rows (if set).

Deleting a parent row deletes referencing child rows too.
Prevents orphans by removing dependents.

37
New cards

ON DELETE SET DEFAULT

ex. Set Instructor_id to 'Staff' when instructor leaves.

Deleting parent sets child’s FK to a default value.
Default must still be valid (exist in parent).

38
New cards

ON DELETE SET

Sets to a specific valid value when parent deleted.
Idea: keep child row, but point it somewhere safe.

ex. Instructor_id becomes 'Staff'.

39
New cards

ON UPDATE CASCADE

If parent key value changes, update matching foreign keys automatically.
Keeps references consistent.

ex. Netid changes → SECTION.Instructor_id updates.

40
New cards

Restrict delete (default idea)

Normally you can’t delete a parent row if children reference it.
Prevents creating orphans.

ex. Can’t delete PERSON if SECTION points to them (unless action set).

41
New cards

CHECK constraint

ex. Year CHECK (Year >= 1968).

Boolean expression that must be true for new/updated values.
Used for validation.

42
New cards

Validation by DB

Constraints are the DB doing validations for you.
Prevents bad data from being stored.

ex. Reject Credit_hours >= 6 if CHECK says < 6.

43
New cards

ALTER TABLE

ex. ALTER TABLE PERSON ADD COLUMN …

Changes an existing table definition.
Can add/drop columns or add constraints.

44
New cards

DROP

Removes a schema object; opposite of CREATE.
Can remove tables, domains, schemas.

45
New cards

DROP TABLE

ex. DROP TABLE SECTION;

Deletes the table definition and its data.
Table does not need to be empty.

46
New cards

DROP TABLE IF EXISTS

ex. DROP TABLE IF EXISTS PERSON;

Avoids an error if the table doesn’t exist.
Safer in scripts.

47
New cards

DROP TABLE RESTRICT

Don’t drop the table if something else references it.
Prevents breaking dependencies.

ex. FK in another table blocks drop.

48
New cards

DROP TABLE CASCADE

Drop dependent relationships/constraints too.
More destructive; removes references.

ex. Drops FK constraint referencing the table.

49
New cards

INSERT INTO

ex. INSERT INTO PERSON …

Adds new rows to a table.
DB checks constraints; rejects violations.

50
New cards

INSERT simple form

INSERT INTO Table VALUES (…) in column-definition order.
Easy but order-dependent.

ex. VALUES follow the table’s column order.

51
New cards

INSERT named form

ex. Insert only required fields.

INSERT INTO Table(col1,col2,…) VALUES (…).
Lets you choose column order and omit optional columns.

52
New cards

INSERT multirow form

Insert multiple rows in one statement.
Uses multiple parenthesized row value sets.

ex. (…), (…), (…).

53
New cards

Required attributes for insert

You must provide keys and NOT NULL columns.
Defaults can be omitted.

ex. Netid + Name + Email required in PERSON example.

54
New cards

Constraint rejection

DB rejects inserts that violate constraints.
Prevents duplicates and invalid values.

ex. Duplicate primary key insert fails.

55
New cards

SELECT

A “statement”(or command) to retrieve data.
Structure: SELECT … FROM … WHERE …

ex. SELECT Course FROM SECTION WHERE …

56
New cards

SELECT clause

ex. SELECT Name, Email.

What columns/expressions you want returned.
This is what comes back.

57
New cards

FROM clause

ex. FROM PERSON, SECTION.

What table(s) you’re working with.
Lists tables used by SELECT/WHERE.

58
New cards

WHERE clause

ex. WHERE Year = 2025 AND Semester='Fall'.

Conditions that filter rows.
Optional; without it you get all rows.

59
New cards

SELECT *

ex. SELECT * FROM SECTION;

Returns entire rows (all columns).
Convenient but often more data than needed.

60
New cards

Table alias

Short name for a table in a query.
Makes queries shorter and avoids ambiguity.

ex. FROM PERSON P, SECTION S.

61
New cards

Qualified attribute (Table.Column)

Used when multiple tables have columns with same names.
Prevents ambiguity.

ex. PERSON.Id vs SECTION.Id.

62
New cards

Join condition (implicit join)

A WHERE condition that matches rows across two tables.
Usually FK = PK.

ex. P.Netid = S.Instructor_id.

63
New cards

Join (concept)

Combines related rows from multiple tables.
Gets you attributes from both together.

ex. PERSON + SECTION data in one result.

64
New cards

Comparison operators

Operators used in WHERE.
Includes =, <>, <, >, <=, >=.

ex. Year < 2000.

65
New cards

Equality operator (=)

Tests equality in SQL.
Single equals sign (not ==).

ex. WHERE Name = 'Chiikawa'.

66
New cards

“<>”

ex. WHERE Department <> 'CS'.

SQL not-equal operator.
(Some DBs also allow !=.)

67
New cards

AND

ex. Year=2025 AND Semester='Fall'.

Combines conditions; both must be true.
Used to narrow results.

68
New cards

OR

ex. Semester='Fall' OR Semester='Spring'.

Combines conditions; either can be true.
Used to broaden results.

69
New cards

NOT

ex. NOT (Year < 2000).

Negates a condition.
With NULL logic, NOT UNKNOWN stays UNKNOWN.

70
New cards

LIKE

ex. WHERE Name LIKE '%Chiikawa_n'.

Pattern match for strings.
Supports _ (one char) and % (wildcard).

71
New cards

_ wildcard

ex. 'Chiikawa_n' matches Chiikawa/Chiikawe.

Matches exactly one character in LIKE.
Used for “one char differs” patterns.

72
New cards

% wildcard

ex. '%This%'

Matches 0 or more characters in LIKE.
Used for “contains” or “starts with” patterns.

73
New cards

Pattern matching intuition

LIKE patterns must match the whole string, but % can cover the rest.
Helps explain why some patterns don’t match.

ex. 'is a %' doesn’t match 'This is a test' (wrong start).

74
New cards

BETWEEN

ex. Credit_hours BETWEEN 1 AND 2 includes 1 and 2.

Checks if a value is in an inclusive range.
Use parentheses to be clear.

75
New cards

Inclusive range

BETWEEN includes endpoints.
Common exam detail.

ex. BETWEEN 1 AND 2 matches 1, 1.5, 2.

76
New cards

ORDER BY

ex. ORDER BY Year DESC.

Sorts query results.
Default ascending; DESC for descending.

77
New cards

Ascending (default)

ex. ORDER BY Year.

ORDER BY sorts smallest→largest by default.
If no DESC, assume ASC.

78
New cards

DESC

ex. ORDER BY Year DESC.

Descending order.
Reverses sort.

79
New cards

Multiple ORDER BY keys

ex. ORDER BY LastName, FirstName.

You can sort by multiple attributes.
Second key breaks ties for the first.

80
New cards

DISTINCT

ex. SELECT DISTINCT Year FROM SECTION.

Removes duplicates in query results.
Useful when selecting non-key attributes.

81
New cards

Duplicates in results

ex. Many sections in same year → repeated Year values.

If you don’t select a key, multiple rows may produce the same output values.
DISTINCT removes repeated outputs.

82
New cards

NULL

Represents missing/unknown/unavailable/not applicable.
Not equal to anything (even to itself).

83
New cards

NULL meanings

NULL can mean unknown, unavailable, or not applicable.


Reason comparisons get tricky.
ex. Unknown phone vs phone doesn’t exist.

84
New cards

NULL equality issue

You can’t use = or <> with it.
Two of them might be the same, but SQL treats it as unknown.

85
New cards

IS NULL

Checks whether a column value is NULL.
Use IS / IS NOT, not =.

ex. WHERE Phone_number IS NULL.

86
New cards

IS NOT NULL

ex. WHERE Phone_number IS NOT NULL.

Checks whether a column value is not NULL.
Useful for filtering to known values.

87
New cards

Three-valued logic (3VL)

SQL uses TRUE, FALSE, and UNKNOWN.
UNKNOWN usually means “don’t select the row.”
ex. Condition with NULL becomes UNKNOWN.

88
New cards

UNKNOWN (3VL meaning)

Result when SQL can’t decide true/false due to NULL.
Generally treated as not selected in WHERE.

ex. Salary > 50000 is UNKNOWN if Salary is NULL.

89
New cards

AND with UNKNOWN

If either side is FALSE → FALSE; if both TRUE → TRUE; otherwise UNKNOWN.
This is why some rows disappear.
ex. TRUE AND UNKNOWN = UNKNOWN.

90
New cards

OR with UNKNOWN

If either side is TRUE → TRUE; if both FALSE → FALSE; otherwise UNKNOWN.
UNKNOWN can still become TRUE if other side is TRUE.
ex. TRUE OR UNKNOWN = TRUE.

91
New cards

NOT with UNKNOWN

NOT TRUE = FALSE; NOT FALSE = TRUE; NOT UNKNOWN = UNKNOWN.
Because you still don’t know.
ex. NOT UNKNOWN stays UNKNOWN.

92
New cards

IN operator

ex. WHERE Letter_grade IN ('A','B','C').

Matches an attribute against a defined set of values.
Works for numbers and strings.

93
New cards

IN (set membership)

Cleaner than a long chain of ORs.
Often used for categories.
ex. Grade IN ('A+','A','A-').

94
New cards

Aggregation

The process of combining individual data items into a grouped summary for analysis

SQL can compute summary stats: SUM, MIN, MAX, COUNT, AVG.
These are written in SELECT (what you want back).
ex. SELECT COUNT(*) FROM EMPLOYEE.

95
New cards

SUM

Adds numeric values across matched rows.
Used for totals.
ex. Total salary paid per year.

96
New cards

COUNT(*)

Counts rows.

* means “all rows,” not “all columns.”
ex. Total number of employees.

97
New cards

COUNT(DISTINCT X)

Counts unique values of X.
Used to count distinct categories.
ex. Number of departments.

98
New cards

MAX

Largest value across matched rows.
Used for top values.
ex. Maximum salary.

99
New cards

MIN

Smallest value across matched rows.
Used for lowest values.
ex. Minimum salary.

100
New cards

AVG

Average value across matched rows.
Used for means.
ex. Average salary.