/ Published in: SQL
A few examples of creating a primary key and a foreign key
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/* Primary Key */ CREATE TABLE employee ( employee_id NUMBER(10), first_name VARCHAR2(100), last_name VARCHAR2(100), salary NUMBER(8), department_id NUMBER(10), CONSTRAINT employee_pk PRIMARY KEY (employee_id) ); ALTER TABLE employee ADD CONSTRAINT employee_pk PRIMARY KEY (employee_id); ALTER TABLE employee DROP CONSTRAINT employee_pk; ALTER TABLE employee DISABLE CONSTRAINT employee_pk; ALTER TABLE employee ENABLE CONSTRAINT employee_pk; /*Foreign key */ CREATE TABLE employee ( employee_id NUMBER(10), first_name VARCHAR2(100), last_name VARCHAR2(100), salary NUMBER(8), department_id NUMBER(10), CONSTRAINT fk_emp_deptid FOREIGN KEY (department_id) REFERENCES department(department_id) ); ALTER TABLE employee ADD CONSTRAINT fk_emp_deptid FOREIGN KEY (department_id) REFERENCES department(department_id); ALTER TABLE employee DROP CONSTRAINT fk_emp_deptid; ALTER TABLE employee DISABLE CONSTRAINT fk_emp_deptid; ALTER TABLE employee ENABLE CONSTRAINT fk_emp_deptid;
URL: https://www.databasestar.com/primary-key-vs-foreign-key