Posted By

bbrumm on 07/27/17


Tagged

sql Oracle


Versions (?)

Primary Key and Foreign Key


 / Published in: SQL
 

URL: https://www.databasestar.com/primary-key-vs-foreign-key

A few examples of creating a primary key and a foreign key

  1. /* Primary Key */
  2. CREATE TABLE employee (
  3. employee_id NUMBER(10),
  4. first_name VARCHAR2(100),
  5. last_name VARCHAR2(100),
  6. salary NUMBER(8),
  7. department_id NUMBER(10),
  8. CONSTRAINT employee_pk PRIMARY KEY (employee_id)
  9. );
  10.  
  11.  
  12.  
  13. ALTER TABLE employee
  14. ADD CONSTRAINT employee_pk PRIMARY KEY (employee_id);
  15.  
  16.  
  17.  
  18. ALTER TABLE employee DROP CONSTRAINT employee_pk;
  19.  
  20. ALTER TABLE employee DISABLE CONSTRAINT employee_pk;
  21.  
  22.  
  23. ALTER TABLE employee ENABLE CONSTRAINT employee_pk;
  24.  
  25. /*Foreign key */
  26.  
  27. CREATE TABLE employee (
  28. employee_id NUMBER(10),
  29. first_name VARCHAR2(100),
  30. last_name VARCHAR2(100),
  31. salary NUMBER(8),
  32. department_id NUMBER(10),
  33. CONSTRAINT fk_emp_deptid FOREIGN KEY (department_id) REFERENCES department(department_id)
  34. );
  35.  
  36. ALTER TABLE employee
  37. ADD CONSTRAINT fk_emp_deptid FOREIGN KEY (department_id) REFERENCES department(department_id);
  38.  
  39.  
  40. ALTER TABLE employee DROP CONSTRAINT fk_emp_deptid;
  41.  
  42. ALTER TABLE employee DISABLE CONSTRAINT fk_emp_deptid;
  43.  
  44. ALTER TABLE employee ENABLE CONSTRAINT fk_emp_deptid;

Report this snippet  

You need to login to post a comment.