Midterm Practical Flashcards

1
Q

Create a database

A

CREATE DATABASE databaseName;

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

Open a database

A

USE databaseName;

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

Create a table with attributes

A

CREATE TABLE employee(
employeeID CHAR(11),
ssn CHAR(11),
name VARCHAR(35),
dob DATE,
CONSTRAINT employee_pk PRIMARY KEY(employeeID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

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

Remove a table within the current database

A

DROP tableName;

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

Remove a database

A

DROP DATABASE databaseName;
DROP DATABASE IF EXISTS test;

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

View metadata commands

A

SHOW DATABASES; SHOW TABLES; DESCRIBE tableName;

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

Run a script

A

. filepath.sql

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

INSERT syntax

A

INSERT INTO tableName
(filedlist) VALUES (valuelist);

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

SELECT all values

A

SELECT *
FROM tableName;

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

Select some values

A

SELECT attributes_to_include
FROM tableName;

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

Select with a column alias

A

SELECT attribute as “longer attribute”
FROM tableName;

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

WHERE clause syntax

A

SELECT * | attributes_to_include
FROM tableName
WHERE condition(s);

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

Logical Operators

A

NOT, AND, OR

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

How to find NULL values

A

SELECT employeeID
FROM employee
WHERE name IS NULL;

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

INSERT NULL values

A

INSER INTO tableNAME (test, test2)
VALUES(‘3333’, NULL);

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

SMALLINT

A

-32768 to 32768 OR 0 to 65535

17
Q

DECIMAL

A

DECIMAL(p,s)
§ Example: DECIMAL(5,2) -> ###.##

18
Q

SIGNED VS UNSIGNED

A

UNSIGNED can store only zero and positive numbers in a column. SIGNED can hold zero, negative and positive

19
Q

How to apply a not null constraint

A

title VARCHAR(255) NOT NULL,

20
Q

Set default constraint

A

replacementCost DECIMAL(5,2) NOT NULL DEFAULT 19.99,

21
Q

Relational Operators

A

> Greater Than
= Greater Than or Equal To
< Less Than
<= Less Than or Equal To
= Equal To
<> Not Equal To
!= Not Equal To
IN (list) Contained in comma-separated list
LIKE string Matches string pattern

22
Q

BETWEEN

A

will include values that are greater than, or equal to, the minimum
value and less than, or equal to, the maximum value specified

SELECT * | attribute(s)
FROM table
WHERE attribute BETWEEN min_value AND max_value;

23
Q

USING LIKE with WILDCARDS

A

To specify our pattern we use characters and wildcards
§ Characters must be present
§ Wildcards are placeholders for characters
§ _ means exactly 1 character
§ % means 0 or more characters

24
Q

Using select calculations

A

SELECT title, length, TRUNCATE(length/60, 0), length MOD 60
FROM film;

SELECT title, length AS “Total Minutes”,
TRUNCATE(length/60,0) AS “Hours”, length MOD 60 AS “Minutes”
FROM film;

25
Q

UPDATE syntax

A

Modifies existing user data
General Syntax Format:
UPDATE tableName
SET field1 = value1, …, fieldN = valueN
WHERE condition;

26
Q

DELETE syntax

A

Removes record(s) from a table
General Syntax Format:
DELETE
FROM tableName
WHERE condition(s);

27
Q

ALTER table to add/drop primary key

A

Let’s get rid of the PRIMAY KEY constraint:
ALTER TABLE film
DROP PRIMARY KEY;

Then add it back:
ALTER TABLE film
ADD PRIMARY KEY film_pk (filmID);

28
Q

add/drop columns

A

General Syntax Format:
ALTER TABLE tableName
{ADD|DROP} COLUMN columnspec;

29
Q

changing defaults

A

Defaults are the only “property” that you can alter.
Let’s add a default value of “Not entered.” to the description column in the table:
ALTER TABLE film
ALTER description SET DEFAULT ‘Not entered.’;

And if we change our mind and don’t want it after all:
ALTER TABLE film
ALTER description DROP DEFAULT;

30
Q

Modifying columns

A

ALTER TABLE tableName
MODIFY COLUMN attribute DATATYPE [NOT NULL] [DEFAULT];

31
Q

1NF to BCNF breakdowns

A

1NF — no repeating groups, attr names are unique, 1NF - has primary key, no repeating attributes

2NF — partial dependencies (functional dependencies are only part of the primary key)

3NF— transitive dependencies on non-key elements to non-key elements.

BCNF — check that left hand side of functional dependencies are candidate keys.