cs 331h: sql prelim

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/6

encourage image

There's no tags or description

Looks like no tags are added yet.

7 Terms

1
New cards

UNIQUE vs PRIMARY KEY ?

unique enforces all values in a column to be different from one another.

PK is a type of unique.

main diff is that PK does NOT allow null values while unique DOES.

2
New cards

CASCADE vs RESTRICT when dropping tables using DROP TABLE

RESTRICT makes it so the drop table is successful only if there are no references to the table in any constraints and views.

CASCADE makes it so the drop table deletes the table and propagates to all constraints and views that reference the table.

3
New cards

how do you add columns to a table? what is the default values for that column in rows if they already exist when the column is added?

add cols using:

ALTER TABLE dbname.tablename ADD colname dtype;

by default, it’s initiated to NULL. you can also use keyword DEFAULT to do something.

4
New cards

how to remove cols from a table?

do:

ALTER TABLE dbname.tablename DROP colname cascade/restrict;

where the same cascade/restrict rules apply as for dropping tables

5
New cards

imagine that a table has a col COL1 with default value ’goon’ , but you wanna remove that default value so it initializes to null. how to do it?

do DROP DEFAULT :

ALTER TABLE dbname.tablename ALTER COL1 DROP DEFAULT;

6
New cards

imagine that a table has a col COL1 with no default value (null), but you wanna add a default value so it initializes to ‘goon’. how to do it?

do SET DEFAULT ‘value’ :

ALTER TABLE dbname.tablename ALTER COL1 SET DEFAULT ‘value’;

7
New cards

how do you remove a table constraint?

using DROP CONSTRAINT, like:

ALTER TABLE COMPANY.EMPLOYEE DROP CONSTRAINT EMPSUPERFK CASCADE