Page 23 - SQL
P. 23
A constraint can be added at the table level.
Types of constraints
• Primary Key - prevents a duplicate record in the table
• Foreign Key - points to a primary key from another table
• Not Null - prevents null values from being entered into a column
• Unique - uniquely identifies each record in the table
• Default - specifies a default value
• Check - limits the ranges of values that can be placed in a column
To learn more about constraints, see the Oracle documentation.
Alter Column
ALTER TABLE Employees
ALTER COLUMN StartingDate DATETIME NOT NULL DEFAULT (GETDATE())
This query will alter the column datatype of StartingDate and change it from simple date to datetime
and set default to current date.
Add Primary Key
ALTER TABLE EMPLOYEES ADD pk_EmployeeID PRIMARY KEY (ID)
This will add a Primary key to the table Employees on the field ID. Including more than one column
name in the parentheses along with ID will create a Composite Primary Key. When adding more
than one column, the column names must be separated by commas.
ALTER TABLE EMPLOYEES ADD pk_EmployeeID PRIMARY KEY (ID, FName)
Read ALTER TABLE online: https://riptutorial.com/sql/topic/356/alter-table
https://riptutorial.com/ 5

