1/6
Looks like no tags are added yet.
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.
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.
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.
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
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;
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’;
how do you remove a table constraint?
using DROP CONSTRAINT, like:
ALTER TABLE COMPANY.EMPLOYEE DROP CONSTRAINT EMPSUPERFK CASCADE