Database Reference
In-Depth Information
Note: Constraints can only be added, modified, or dropped at the table
level (out-of-line constraints). The exception to this rule is the NOT NULL
constraint where the column is modified. For example, to add a NOT
NULL constraint to the NAME column, the command is: ALTER TABLE
ARTISTS MODIFY (NAME VARCHAR2(32) NOT NULL).
Once again, let's use the three tables: ARTISTS, SONGS, and
CDTRACKS. First, drop the ARTISTS table again so that we can start
with a clean slate.
DROP TABLE ARTISTS;
Now let's go ahead and create the ARTISTS and SONGS tables without
any constraints.
CREATE TABLE ARTISTS(ARTIST_ID NUMBER, NAME VARCHAR2(32));
CREATE TABLE SONGS(SONG_ID NUMBER, ARTIST_ID NUMBER
, TITLE VARCHAR2(64));
20.3.3.1
Adding a Constraint to an Existing Table
Now we need to add the constraints for the ARTISTS and SONGS tables.
Add a primary key constraint to the ARTISTS table on the ARTIST_ID
column:
ALTER TABLE ARTISTS ADD CONSTRAINT PK_ARTISTS
PRIMARY KEY(ARTIST_ID);
Add a unique constraint to the ARTISTS table on the NAME column:
ALTER TABLE ARTISTS ADD CONSTRAINT UK_ARTISTS UNIQUE(NAME);
Add a primary key constraint to the SONGS table on the SONG_ID
column:
ALTER TABLE SONGS ADD CONSTRAINT PK_SONGS
PRIMARY KEY(SONG_ID);
Search WWH ::




Custom Search