CREATE TABLE ARTIST
( ID NUMBER,
NAME VARCHAR2(255)
)
Table created.
INSERT INTO ARTIST (ID, NAME) VALUES (null, null)
1 row(s) inserted.
COMMIT
Statement processed.
SELECT * FROM ARTIST
ID | NAME | - | - |
---|
DROP TABLE ARTIST
Table dropped.
NOT NULL Constraint:
CREATE TABLE ARTIST
( ID NUMBER NOT NULL,
NAME VARCHAR2(255) NOT NULL
)
Table created.
INSERT INTO ARTIST (ID, NAME) VALUES (null, null)
ORA-01400: cannot insert NULL into ("SQL_ITMDTYOTNHHELLAIUVCURZXOH"."ARTIST"."ID") ORA-06512: at "SYS.DBMS_SQL", line 1707More Details: https://docs.oracle.com/error-help/db/ora-01400
INSERT INTO ARTIST (ID, NAME) VALUES (1, 'New Ones')
1 row(s) inserted.
INSERT INTO ARTIST (ID, NAME) VALUES (2, 'New Ones')
1 row(s) inserted.
SELECT * FROM ARTIST
ID | NAME | 1 | New Ones | 2 | New Ones |
---|
DELETE FROM ARTIST
2 row(s) deleted.
We add an UNIQUE CONSTRAINT for NAME:
ALTER TABLE ARTIST
ADD CONSTRAINT ARTIST_NAME_UK
UNIQUE (NAME)
Table altered.
INSERT INTO ARTIST (ID, NAME) VALUES (1, 'New Ones')
1 row(s) inserted.
INSERT INTO ARTIST (ID, NAME) VALUES (2, 'New Ones')
ORA-00001: unique constraint (SQL_ITMDTYOTNHHELLAIUVCURZXOH.ARTIST_NAME_UK) violated ORA-06512: at "SYS.DBMS_SQL", line 1707More Details: https://docs.oracle.com/error-help/db/ora-00001
INSERT INTO ARTIST (ID, NAME) VALUES (1, 'Adele')
1 row(s) inserted.
SELECT * FROM ARTIST
ID | NAME | 1 | New Ones | 1 | Adele |
---|
DELETE FROM ARTIST
2 row(s) deleted.
The ID is our main key, the PRIMARY KEY:
ALTER TABLE ARTIST
ADD CONSTRAINT ARTIST_PK
PRIMARY KEY (ID)
Table altered.
INSERT INTO ARTIST (ID, NAME) VALUES (1, 'New Ones')
1 row(s) inserted.
INSERT INTO ARTIST (ID, NAME) VALUES (1, 'Adele')
ORA-00001: unique constraint (SQL_ITMDTYOTNHHELLAIUVCURZXOH.ARTIST_PK) violated ORA-06512: at "SYS.DBMS_SQL", line 1707More Details: https://docs.oracle.com/error-help/db/ora-00001
DROP TABLE ARTIST
Table dropped.
Constraints can be created together with the table:
CREATE TABLE ARTIST
( ID NUMBER CONSTRAINT ARTIST_PK PRIMARY KEY,
NAME VARCHAR2(255) NOT NULL CONSTRAINT ARTIST_NAME_UK UNIQUE
)
Table created.
INSERT INTO ARTIST (ID, NAME) VALUES (1, 'New Ones')
1 row(s) inserted.
INSERT INTO ARTIST (ID, NAME) VALUES (2, 'Adele')
1 row(s) inserted.
INSERT INTO ARTIST (ID, NAME) VALUES (3, 'Coldplay')
1 row(s) inserted.
INSERT INTO ARTIST (ID, NAME) VALUES (4, 'Silent')
1 row(s) inserted.
COMMIT
Statement processed.
CREATE TABLE ALBUM
( ID NUMBER PRIMARY KEY,
ARTIST_ID NUMBER,
NAME VARCHAR2(255) NOT NULL CONSTRAINT ALBUM_NAME_UK UNIQUE
)
Table created.
INSERT INTO ALBUM (ID, ARTIST_ID, NAME) VALUES (22, 4, 'Hello')
1 row(s) inserted.
INSERT INTO ALBUM (ID, ARTIST_ID, NAME) VALUES (42, 2, 'Hello')
ORA-00001: unique constraint (SQL_ITMDTYOTNHHELLAIUVCURZXOH.ALBUM_NAME_UK) violated ORA-06512: at "SYS.DBMS_SQL", line 1707More Details: https://docs.oracle.com/error-help/db/ora-00001
DROP TABLE ALBUM
Table dropped.
Two column UNIQUE CONSTRAINT:
CREATE TABLE ALBUM
( ID NUMBER PRIMARY KEY,
ARTIST_ID NUMBER,
NAME VARCHAR2(255) NOT NULL,
CONSTRAINT ALBUM_NAME_UK UNIQUE (ARTIST_ID, NAME)
)
Table created.
INSERT INTO ALBUM (ID, ARTIST_ID, NAME) VALUES (22, 4, 'Hello')
1 row(s) inserted.
INSERT INTO ALBUM (ID, ARTIST_ID, NAME) VALUES (42, 2, 'Hello')
1 row(s) inserted.
SELECT * FROM ALBUM
ID | ARTIST_ID | NAME | 22 | 4 | Hello | 42 | 2 | Hello |
---|
We can drop Constraints:
ALTER TABLE ALBUM DROP CONSTRAINT ALBUM_NAME_UK
Table altered.
... without losing data:
SELECT * FROM ALBUM
ID | ARTIST_ID | NAME | 22 | 4 | Hello | 42 | 2 | Hello |
---|
... and add Constraints to existing tables (with data):
ALTER TABLE ALBUM
ADD CONSTRAINT ALBUM_NAME_UK UNIQUE (ARTIST_ID, NAME)
Table altered.
INSERT INTO ALBUM (ID, ARTIST_ID, NAME) VALUES (22, 4, 'Hello')
ORA-00001: unique constraint (SQL_ITMDTYOTNHHELLAIUVCURZXOH.SYS_C00737536) violated ORA-06512: at "SYS.DBMS_SQL", line 1707More Details: https://docs.oracle.com/error-help/db/ora-00001
ROLLBACK
Statement processed.
INSERT INTO ALBUM (ID, ARTIST_ID, NAME) VALUES (21, 2, 'James Bond 007: Skyfall')
1 row(s) inserted.
INSERT INTO ALBUM (ID, ARTIST_ID, NAME) VALUES (31, 3, 'Ghost Stories')
1 row(s) inserted.
INSERT INTO ALBUM (ID, ARTIST_ID, NAME) VALUES (32, 3, 'A Head Full of Dreams')
1 row(s) inserted.
INSERT INTO ALBUM (ID, ARTIST_ID, NAME) VALUES (41, 4, 'Without a Sound')
1 row(s) inserted.
INSERT INTO ALBUM (ID, ARTIST_ID, NAME) VALUES (51, null, 'From the Unknown')
1 row(s) inserted.
INSERT INTO ALBUM (ID, ARTIST_ID, NAME) VALUES (61, null, 'The Lost')
1 row(s) inserted.
COMMIT
Statement processed.
INSERT INTO ALBUM (ID, ARTIST_ID, NAME) VALUES ('71', 999, 'Songs from the Wrong')
1 row(s) inserted.
We add a FOREIGN CONSTRAINT:
ALTER TABLE ALBUM
ADD CONSTRAINT ALBUM_FK
FOREIGN KEY (ARTIST_ID)
REFERENCES ARTIST (ID)
ORA-02298: cannot validate (SQL_ITMDTYOTNHHELLAIUVCURZXOH.ALBUM_FK) - parent keys not foundMore Details: https://docs.oracle.com/error-help/db/ora-02298
SELECT * FROM ALBUM
ID | ARTIST_ID | NAME | 22 | 4 | Hello | 42 | 2 | Hello | 21 | 2 | James Bond 007: Skyfall | 31 | 3 | Ghost Stories | 32 | 3 | A Head Full of Dreams | 41 | 4 | Without a Sound | 51 | - | From the Unknown | 61 | - | The Lost | 71 | 999 | Songs from the Wrong |
---|
DELETE FROM ALBUM a WHERE a.ID = 71
1 row(s) deleted.
Add FOREIGN KEY – take 2:
ALTER TABLE ALBUM
ADD CONSTRAINT ALBUM_FK
FOREIGN KEY (ARTIST_ID)
REFERENCES ARTIST (ID)
Table altered.
SELECT * FROM ALBUM
ID | ARTIST_ID | NAME | 22 | 4 | Hello | 42 | 2 | Hello | 21 | 2 | James Bond 007: Skyfall | 31 | 3 | Ghost Stories | 32 | 3 | A Head Full of Dreams | 41 | 4 | Without a Sound | 51 | - | From the Unknown | 61 | - | The Lost |
---|
INSERT INTO ALBUM
(ID, ARTIST_ID, NAME)
VALUES (71, 999, 'Songs from the Wrong')
ORA-02291: integrity constraint (SQL_ITMDTYOTNHHELLAIUVCURZXOH.ALBUM_FK) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1707More Details: https://docs.oracle.com/error-help/db/ora-02291
Table SONG with PRIMARY KEY, NOT NULL, UNIQUE Constraint and FOREIGN KEY
CREATE TABLE SONG
( ID NUMBER CONSTRAINT SONG_PK PRIMARY KEY,
ALBUM_ID NUMBER,
NAME VARCHAR2(255) NOT NULL,
CONSTRAINT SONG_NAME_UK UNIQUE (ALBUM_ID, NAME),
CONSTRAINT SONG_NAME_FK FOREIGN KEY (ALBUM_ID) REFERENCES ALBUM (ID)
)
Table created.
INSERT INTO SONG (ID, ALBUM_ID, NAME) VALUES (203, 22, 'Skyfall')
1 row(s) inserted.
INSERT INTO SONG (ID, ALBUM_ID, NAME) VALUES (301, 31, 'Oceans')
1 row(s) inserted.
INSERT INTO SONG (ID, ALBUM_ID, NAME) VALUES (302, 31, 'A Sky Full of Stars')
1 row(s) inserted.
INSERT INTO SONG (ID, ALBUM_ID, NAME) VALUES (303, 32, 'Everglow')
1 row(s) inserted.
INSERT INTO SONG (ID, ALBUM_ID, NAME) VALUES (501, 51, 'Who''s singing?')
1 row(s) inserted.
INSERT INTO SONG (ID, ALBUM_ID, NAME) VALUES (603, null, 'Song for Nobody')
1 row(s) inserted.
COMMIT
Statement processed.
SELECT * FROM ARTIST
ID | NAME | 1 | New Ones | 2 | Adele | 3 | Coldplay | 4 | Silent |
---|
SELECT * FROM ALBUM
ID | ARTIST_ID | NAME | 22 | 4 | Hello | 42 | 2 | Hello | 21 | 2 | James Bond 007: Skyfall | 31 | 3 | Ghost Stories | 32 | 3 | A Head Full of Dreams | 41 | 4 | Without a Sound | 51 | - | From the Unknown | 61 | - | The Lost |
---|
SELECT * FROM SONG
ID | ALBUM_ID | NAME | 203 | 22 | Skyfall | 301 | 31 | Oceans | 302 | 31 | A Sky Full of Stars | 303 | 32 | Everglow | 501 | 51 | Who's singing? | 603 | - | Song for Nobody |
---|
Test of Foreign Key Constraint:
INSERT INTO SONG (ID, ALBUM_ID, NAME)
VALUES (901, 9999999, 'Wrong Key')
ORA-02291: integrity constraint (SQL_ITMDTYOTNHHELLAIUVCURZXOH.SONG_NAME_FK) violated - parent key not found ORA-06512: at "SYS.DBMS_SQL", line 1707More Details: https://docs.oracle.com/error-help/db/ora-02291
Drop a Foreign Key Constraint:
ALTER TABLE SONG DROP CONSTRAINT SONG_NAME_FK
Table altered.
Drop the Primary Key Constraint:
ALTER TABLE ALBUM DROP PRIMARY KEY
Table altered.
Table with Check Constraints:
CREATE TABLE PERSON (
ID NUMBER CONSTRAINT PERSON_PK PRIMARY KEY,
NAME VARCHAR2(255) CONSTRAINT PERSON_NAME_LENGTH_CHK CHECK (length(NAME) > 2),
AGE NUMBER(3) CONSTRAINT PERSON_AGE_CHK CHECK (AGE BETWEEN 0 AND 125),
SEX VARCHAR2(1)
)
Table created.
Add another Check Constraint:
ALTER TABLE PERSON ADD CONSTRAINT PERSON_SEX_CHK CHECK (SEX IN ('m', 'f'))
Table altered.
INSERT INTO PERSON (ID, NAME, AGE, SEX) VALUES (1, 'x', 28, 'm')
ORA-02290: check constraint (SQL_ITMDTYOTNHHELLAIUVCURZXOH.PERSON_NAME_LENGTH_CHK) violated ORA-06512: at "SYS.DBMS_SQL", line 1707More Details: https://docs.oracle.com/error-help/db/ora-02290
INSERT INTO PERSON (ID, NAME, AGE, SEX) VALUES (2, 'Adams', 135, 'f')
ORA-02290: check constraint (SQL_ITMDTYOTNHHELLAIUVCURZXOH.PERSON_AGE_CHK) violated ORA-06512: at "SYS.DBMS_SQL", line 1707More Details: https://docs.oracle.com/error-help/db/ora-02290
INSERT INTO PERSON (ID, NAME, AGE, SEX) VALUES (3, 'Bond', 14, 'x')
ORA-02290: check constraint (SQL_ITMDTYOTNHHELLAIUVCURZXOH.PERSON_SEX_CHK) violated ORA-06512: at "SYS.DBMS_SQL", line 1707More Details: https://docs.oracle.com/error-help/db/ora-02290
INSERT INTO PERSON (ID, NAME, AGE, SEX) VALUES (4, 'Smith', 23, 'f')
1 row(s) inserted.
SELECT * FROM PERSON
ID | NAME | AGE | SEX | 4 | Smith | 23 | f |
---|