primary key and code example

Example 1: what is primary key

PRIMARY KEY  -- unique identifier for the entire row of record in a table 
             -- can not be null and must be unique

Example 2: MySQL Primary Key

Typically, you define the primary key for a table in the CREATE TABLE statement.

If the primary key has one column, you can use the PRIMARY KEY constraint as a column constraint:

CREATE TABLE table_name(
    primary_key_column datatype PRIMARY KEY,
    ...
);

Example 3: primary key

Primary Key :
It is unique column in every table in a database
It can ONLY accept;
    - nonduplicate values
    - cannot be NULL

Example 4: identify primary key in sql table

-- NOTE: this is for SQL-Oracle specifically

-- syntax:
SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner
FROM all_constraints cons, all_cons_columns cols
WHERE cols.table_name = '<table-name>' -- Replace <table-name> with your table-name
AND cons.constraint_type = 'P'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner


-- example:
SELECT cols.table_name, cols.column_name, cols.position, cons.status, cons.owner
FROM all_constraints cons, all_cons_columns cols
WHERE cols.table_name = 'CUSTOMERS' 
AND cons.constraint_type = 'P'
AND cons.constraint_name = cols.constraint_name
AND cons.owner = cols.owner

Tags:

Sql Example