Relational DB Flashcards
- What are the three components of a database model?
Data structure → describes how data is organized
Operations → manipulates data structures
Rules → govern valid data
- What is a tuple?
a tuple is an ordered collection
- What is a set?
a set is an unordered collection of elements enclosed in braces
- {a,b,c} and {c,b,a} are the same because order doesn’t matter
- How is a relational database structure organized?
a table has a name, a fixed tuple of columns, and varying sets of rows
- a column has a name and a data type
- a row is an unnamed tuple of values. each value corresponds to a column and belongs to the columns data type
- a data type is a named set of values from which column values are drawn
Describe ‘select’
Selectselects a subset of rows of a table.
Describe ‘project’
Project eliminates one or more columns of a table.
- Describe ‘product’
- Productlists all combinations of rows of two tables.
- Describe ‘join’
- Joincombines two tables by comparing related columns.
- Describe ‘union’
- Unionselects all rows of two tables.
- Describe ‘intersect’
Intersect selects rows common to two tables.
- Describe ‘difference’
Difference selects rows that appear in one table but not another.
- Describe ‘rename’
- Renamechanges a table name.
- Describe ‘aggregate’
- Aggregate computes functions over multiple table rows, such as sum and count.
- What are the three logical constraints in relational database management that ensure data is valid?
- Unique primary key → all tables have a primary key column, or group of columns, in which values may not repeat
- Unique column names → Different columns of the same table have different names
- No duplicate rows → No two rows of the same table have identical values in all columns
- What does SQL stand for?
Structured Query Language
- What is a ‘statement’?
- A statement is a complete command composed of one or more clauses
- What is a ‘clause’?
- A clause groups SQL keywords like SELECT, FROM, and WHERE with table names
- What are some key words in SQL?
SELECT, FROM, WHERE
- How do you end a statement in MySQL?
a semi-colon, “ ; “
- What is DDL and what is it responsible for?
- Data definition language (DDL) → Defines the structure of the database
- What is DQL and what is it responsible for?
- Data query language (DQL) → Retrieves data from the database
- What is DML and what is it responsible for?
- Data manipulation language (DML)→ Manipulates data stored in a database
- What is DCL and what is it responsible for?
- Data control language (DCL) → Controls database user access
- What is DTL and what is it responsible for?
- Data transaction language (DTL) → Manages database transactions. commits data to a database, rolls back data from a database, and creates save points.
- Describe the statement
CREATE DATABASE {database name}
creates a new db
- Describe the statement
DROP DATABASE {database name}
deletes a db, including all tables
- Describe the keyword SHOW
provides information about database and its contents
- Describe some commonly used SHOW statements
- SHOW DATABASES → lists dbs available in the system
- SHOW TABLES → lists tables available in the currently selected db
- SHOW COLUMNS → Lists columns in a specified table named by a FROM clause
- SHOW CREATE TABLE → shows the CREATE TABLE statement for a given table
- In relational databases, how many values can exist in one cell?
1
- Are duplicate column names allowed?
no
- Are duplicate rows allowed?
no
- How does row order affect query status?
- No row order. The organization of the rows never affects query results data independence
Define data independence
- The result of a database query is not affected by the physical organization of data on storage devices
- Describe the Integer data type
- Integerdata types represent positive and negative integers. Several integer data types exist, varying by the number of bytes allocated for each value. Common integer data types include INT, implemented as 4 bytes of storage, and SMALLINT, implemented as 2 bytes.
- Describe the decimal data type
- Decimaldata types represent numbers with fractional values. Decimal data types vary by number of digits after the decimal point and maximum size. Common decimal data types include FLOAT and DECIMAL.
- Describe the Character data type
- Characterdata types represent textual characters. Common character data types include CHAR, a fixed string of characters, and VARCHAR, a string of variable length up to a specified maximum size.
- Describe the Date and time data types
- Date and timedata types represent date, time, or both. Some date and time data types include a time zone or specify a time interval. Some date and time data types represent an interval rather than a point in time. Common date and time data types include DATE, TIME, DATETIME, and TIMESTAMP.
- Describe the Binary data type
- Binarydata types store data exactly as the data appears in memory or computer files, bit for bit. The database manages binary data as a series of zeros and ones. Common binary data types include BLOB, BINARY, VARBINARY, and IMAGE.
- Describe the Spatial data type
- Spatialdata types store geometric information, such as lines, polygons, and map coordinates. Examples include POLYGON, POINT, and GEOMETRY. Spatial data types are relatively new and consequently vary greatly across database systems.
- Describe the Document data type
- Document data types contain textual data in a structured format such as XML or JSON.
- Describe the MySQL datatype TINYINT
1 byte Signed range: -128 to 127
Unsigned range: 0 to 255
- Describe the MySQL datatype SMALLINT
2 bytes Signed range: -32,768 to 32,767
Unsigned range: 0 to 65,535
- Describe the MySQL datatype MEDIUMINT
3 bytes Signed range: -8,388,608 to 8,388,607
Unsigned range: 0 to 16,777,215
- Describe the MySQL datatype INTEGER or INT
4 bytes Signed range: -2,147,483,648 to 2,147,483,647
Unsigned range: 0 to 4,294,967,295
- Describe the MySQL datatype DECIMAL(M,D)
DECIMAL(M,D) Varies depending on M and D Exact decimal number where M = number of significant digits, D = number of digits after decimal point
- Describe the MySQL datatype FLOAT
4 bytes Approximate decimal numbers with range: -3.4E+38 to 3.4E+38
- Describe the MySQL datatype DOUBLE
8 bytes Approximate decimal numbers with range: -1.8E+308 to 1.8E+308
- Describe the MySQL datatype DATE
3 bytes Format: YYYY-MM-DD. Range: ‘1000-01-01’ to ‘9999-12-31’
- Describe the MySQL datatype TIME
3 bytes Format: hh:mm:ss
- Describe the MySQL datatype DATETIME
5 bytes Format: YYYY-MM-DD hh:mm:ss. Range: ‘1000-01-01 00:00:00’ to ‘9999-12-31 23:59:59’.
- Describe the MySQL datatype CHAR(N)
N bytes Fixed-length string of length N; 0 ≤ N ≤ 255
- Describe the MySQL datatype VARCHAR(N)
Length of characters + 1 bytes Variable-length string with maximum N characters; 0 ≤ N ≤ 65,535
- Describe the MySQL datatype TEXT
Length of characters + 2 bytes Variable-length string with maximum 65,535 characters
- What is an operator?
A symbol that computes a value from one or more other values called operands
- What is an operand?
- Describe an arithmetic operator
operators compute numeric values from numeric operands
- Describe a comparison operator
compute logical values TRUE and FALSE. Operands can be numeric, character, and other data types.
- Describe a logical operator
operators compute logical values from logical operands
- Describe operator precedence
is the order in which an expression is operated
- Describe the logical operator AND
- Returns TRUE when only both values are TRUE
- Describe the logical operator OR
- Returns FALSE when only both values are FALSE
- Describe the logical operator NOT
- Reverses a logical value
- Describe NULL
- Unknown or inapplicable data
- Describe NOT NULL
- A constraint added to columns which prevents a NULL value being added as data
- How do you return a row with a value of NULL?
- WHERE ___ IS NULL. ‘IS NULL’
- Describe MySQL truth table
- In MySQL how does x=TRUE AND y=NULL evaluate?
null
- In MySQL how does x=NULL AND y=TRUE evaluate?
null
- In MySQL how does x=TRUE OR y=NULL evaluate?
true
- In MySQL how does x=NULL OR y=TRUE evaluate?
true
- In MySQL how does x=FALSE OR y=NULL evaluate?
null
- In MySQL how does x=NULL OR y=FALSE evaluate?
null
- In MySQL how does x=FALSE AND y=NULL evaluate?
false
- In MySQL how does x=NULL AND y=FALSE evaluate?
false
- In MySQL how does x=NULL AND y=NULL evaluate?
null
- In MySQL how does x=NULL OR y=NULL evaluate?
null
- Describe the INSERT statement
- Adds a row to a table
Contains two clauses:-
INSERT INTO
clause names the table and columns where data is to be added.- Note that
INTO
is optional
- Note that
-
VALUES
clause specifies the column values to be added- May list any number of rows in parentheses to insert multiple rows
-
- Describe the INTO keyword
-
INSERT INTO
clause names the table and columns where data is to be added. - Note that
INTO
is optional
- Describe DEFAULT clause
The DEFAULT
key word can be used on column creation to specify a value if one is not provided when INSERT-ing a new column
- Describe the UPDATE statement
-
UPDATE
modifies existing rows
- What clause is used with an UPDATE statement
the SET
clause is used to specify the new value
- Describe the WHERE clause
Specifies a condition
- Describe a primary key
- A column or group of columns used to identify a row
- Usually the tables first column, but the position isn’t significant
- Describe the characteristics of a primary key
- Primary keys must be unique
- Primary keys must not be NULL
- Describe composite primary keys
- A composite primary key is used when multiple columns have to identify a row
- denoted with parentheses:
(ColumnA, ColumnB)
- What is the PRIMARY KEY constraint?
- names the table’s primary key
- Ensures that a column or group of columns is always unique and non-null
- Describe AUTO_INCREMENT
numeric column assigned an automatically incrementing value when a new row is inserted
- Describe referential integrity
relational rule that requires foreign keys to either be fully NULL or match primary key values
- Describe the concept ‘fully NULL’
a foreign key in which all columns are null
- Consider the opposite, partially null, in a composite key context
- What are the 4 ways referential integrity can be violated?
- A primary key is updated
- A foreign key is updated
- A row containing a primary key is deleted
- A row containing a foreign key is inserted
- What are the 4 constraints usually used to maintain referential integrity?
-
RESTRICT
→ rejects an insert, update, or delete that violates referential integrity -
SET NULL
→ sets invalid foreign keys to NULL -
SET DEFUALT
→ sets invalid foreign keys to the default foreign key value -
CASCADE
→ propagates primary key changes to foreign keys- Matching foreign key rows are deleted
- Describe
RESTRICT
rejects an insert, update, or delete that violates referential integrity
- Describe
SET NULL
sets invalid foreign keys to NULL
- Describe
SET DEFAULT
sets invalid foreign keys to the default foreign key value
- Describe
CASCADE
propagates primary key changes to foreign keys. Matching foreign key rows are deleted
- What does MySQL allow for maintaining referential integrity as far as inserting or updating foreign keys?
For foreign key inserts and updates MySQL supports only RESTRICT
- Describe ‘constraints’
rule that governs allowable values in a db
- If a statement would violate a constraint it is rejected
- What is a column constraint?
- appears after column name and data type in
CREATE TABLE
statement
- What is a table constraint?
- Appears in a statement other than CREATE TABLE and governs values in one or more columns
- What is the UNIQUE constraint?
- Ensures that a values in a column or group of columns is unique
- Describe the CHECK constraint
- Specifies an expression on one or more columns, and returns TRUE or FALSE
- When is the CHECK constraint considered a table constraint?
If the CHECK
involves multiple columns, it is considered a table constraint and must be declared in a separate clause.