Session 2 - Creating, querying data Flashcards

1
Q

What is the syntax for adding a column to the end of a table?

A

ALTER TABLE tableName

ADD columnName;

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

What is the syntax for updating the column type?

A

ALTER TABLE tableName

ALTER COLUMN columnName datatype

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

What is the DELETE Syntax?

A

DELETE
FROM
WHERE

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

Syntax for adding Unique constraint to column

A

Alter table MyTable

ADD CONSTRAINT nameOfConstraint UNIQUE (columns)

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

Syntax for adding a Default Constraint to a new table

A

columnName dataType (not) null CONSTRAINT constraintName DEFAULT defaultValue

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

Syntax for adding a Default constraint to an existing column

A

ALTER TABLE tblName

ADD CONSTRAINT constraintName DEFAULT defaultValue FOR columnName

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

Syntax for adding a check constraint but to not check the previous rows to verify that they validate the check

A

ALTER TABLE myTable WITH NOCHECK

ADD CONSTRAINT myConstraint CHECK (col1 = ‘test’)

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

What is an identity?

A

Used with PRIMARY KEY and will automatically set a number value. IDENTITY(startingValue, incrementValue)

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

What is a Primary Key?

A

Identifying value of a table. It will cluster (order) the table based on this value.
Not Nullable
One Per table
–Surrogate keys are typically numbers that have nothing to do with real life
–Natural Key (Such as Email Address) are naturally keys

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

Syntax for creating Primary Key

A

CREATE TABLE tableName (
employeeNumber int CONSTRAINT constraintName PRIMARY KEY IDENTITY(startingValue, incrementValue)
)

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

Difference between DELETE TABLE and TRUNCATE TABLE regarding Primary Key IDENTITY

A

DELETING all records allows IDENTITY to keep the next value and TRUNCATE TABLE starts the Identity over

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

Syntax for adding Primary Key to existing column

A

ALTER TABLE tableName
ADD CONSTRAINT constraintName PRIMARY KEY (columnName)

You can add NONCLUSTERED if you don’t want it to be clustered (ordered)

ALTER TABLE tableName
ADD CONSTRAINT constraintName PRIMARY KEY NONCLUSTERED (columnName)

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

What are the effects that you can set when changing a primary key that has foreign keys attached?

A

NO ACTION
CASCADE
SET NULL
SET DEFAULT

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

what is NO ACTION for Primary Key change?

A

Nothing will change in the foreign keys referencing the primary key. Raises error

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

what is CASCADE for Primary Key change?

A

Changes will reflect on the foreign keys columns

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

what is SET NULL for Primary Key change?

A

The foreign key column will be set to null

17
Q

what is SET DEFAULT for Primary Key change?

A

It will be set to the default value specified

18
Q

How do you produce Sub Total Rows from query?

A

SELECT COL1, COL2, COL3
FROM TABLE
GROUP BY ROLLUP(COL1, COL2, COL3)

19
Q

How do you show if a column is grouped in a ROLLUP row?

A

SELECT COL1, COL2, COL3, GROUPING(COL1) AS COL1_GRP
FROM TABLE
GROUP BY ROLLUP(COL1, COL2, COL3)

WILL SHOW 1 IF COLUMN

20
Q

What are the two types of primary keys?

A
Surrogate keys are typically numbers that have nothing to do with real life
Natural Keys (Such as Email Address) are naturally keys
21
Q

How can you tell which columns are being grouped when using ROLLUP, CUBE OR GROUPING SETS?

A

USE GROUPING_ID(COL1, COL2, COL3)

The return will be binary from right to left:

COL3 grouped would be 1,
COL2 - 2
COL1 - 4

if COL1 and COL3 were grouped then the return value would be 5.

22
Q

How can you group according to sets specified?

A

use GROUP BY GROUPING SETS((COL1, COL2, COL3), (COL2, COL3), ())

*() denotes all columns

23
Q

What is the SQL-92 Syntax for joins?

A

That is the currently accepted standard:

SELECT *
FROM TABLE1 A
JOIN TABLE2 B
ON A.COL1 = B.COL1

24
Q

What is the SQL-89 Syntax for Joins

A

That is the older, legacy join syntax:

SELECT *
FROM TABLE1 A, TABLE2 B
WHERE A.COL1 = B.COL1

25
Q

What are the benefits of using SQL-92 for joins over SQL-89?

A

SQL-92 syntax is easier to understand
SQL-92 is the current best practice
SQL-92 is less error prone

26
Q

Is SQL-92 more efficient than SQL-89 for joining two or more tables?

A

No, there is no proven efficiency and it is no less code than SQL-89.