Becca Bohem Becca Bohem - 7 months ago 11
SQL Question

Oracle SQL: Creating a Primary Key From Created Field

Say I have the table...

Name Null Type
--------- ---- ------------
BOOK_CODE VARCHAR2(26)
AUTHOR_ID NUMBER(2)
SEQUENCE NUMBER(1)


It's already been created, how would I go about making
BOOK_CODE
the primary Key? The only way I can find is to delete the field and then replace it, but I would lose all the data I put in it that way.

Any IDeas?

Answer
ALTER TABLE <TABLENAME> ADD CONSTRAINT pk_book_code PRIMARY KEY(book_code);

For foreign keys:

ALTER TABLE table_name
ADD CONSTRAINT constraint_name
   FOREIGN KEY (column1, column2, ... column_n)
   REFERENCES parent_table (column1, column2, ... column_n);
Comments