Quiz 2 - SQL/Create Database Flashcards
SQL: CREATE TABLE syntax
CREATE TABLE table_name ( column_name1 data_type(size), column_name2 data_type(size), column_name3 data_type(size), ... );
SQL: CREATE TABLE syntax - w/constraints
CREATE TABLE table_name
(
column_name1 data_type(size) constraint_name,
column_name2 data_type(size) constraint_name,
column_name3 data_type(size) constraint_name,
….
);
SQL: ALTER TABLE syntax - add column
ALTER TABLE table_name
ADD COLUMN column_name datatype
;
SQL: ALTER TABLE syntax - drop column
ALTER TABLE table_name
DROP COLUMN column_name
;
SQL: ALTER TABLE syntax - change datatype
ALTER TABLE table_name
ALTER COLUMN column_name datatype
;
SQL: ALTER TABLE syntax - add constraint
ALTER TABLE table_name
ADD CONSTRAINT constraint_name constraint_keyword (column_name)
;
Constraint Keywords
- NOT NULL
- UNIQUE
- PRIMARY KEY
- FOREIGN KEY
- CHECK
- DEFAULT
SQL: ALTER TABLE syntax - add Foreign Key
ALTER TABLE Orders ADD CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id) REFERENCES Persons(P_Id) ;
SQL: ALTER TABLE syntax - add CHECK
ALTER TABLE Persons
ADD CONSTRAINT chk_Person CHECK (P_Id>0)
;
SQL: ALTER TABLE syntax - add DEFAULT
ALTER TABLE Persons
ALTER COLUMN City SET DEFAULT ‘Fairfax’
;