The following are the main constraints which can be defined when you are creating a TABLE. I will be using the ORACLE DBMS .
- Primary key – The primary key of the table to be created.
- Foreign key – The primary key of some other table which is being referenced in this table to be created. (The other table should already exist with the column being referenced before calling it in this creation statement)
- Unique – unique (for candidate keys)
- Required (NOT NULL) – No Null Values are allowed
- Check – The column should be some specified createria –
and you can either place the contrainst specification as either :
- External: after column definitions
- Inline: same line as a column definition
All of these constraints can be external or inline in Oracle. Typically required (NOT NULL) constraints are inline and others are external.
Here is a basic TABLE creation syntax in Oracle
--CREATE HOTEL LOCATION TABLE CREATE TABLE HOTEL_LOCATION( LOCATION_ID INT, ISCITY CHAR(1) CONSTRAINT HL_ISCITY NOT NULL, NAME VARCHAR2(255), DISTANCE_FROM_AIRPORT INTEGER, CONSTRAINT HL_LOCATION_ID PRIMARY KEY (LOCATION_ID) ); --CREATE HOTEL TABLE CREATE TABLE HOTEL_1( HOTELID INTEGER, HOTELNAME VARCHAR(255), DESCRIPTION VARCHAR(255), IS_FIVE_STAR CHAR(1), LOCATION_ID INT, NUMROOMS INTEGER, CONSTRAINT PK_HOTEL_1 PRIMARY KEY (HOTELID), CONSTRAINT FK_LOCATION_ID FOREIGN KEY (LOCATION_ID) REFERENCES HOTEL_LOCATION, CONSTRAINT HOTEL_1 CHECK (NUMROOMS BETWEEN 1 AND 325) );
Here is the table created with the specified constraints