0% found this document useful (0 votes)
4 views3 pages

L3 MySQL Primary Key, Auto - Increment and Excercise

The document provides examples of creating tables in SQL with a focus on defining primary keys and using the AUTO_INCREMENT feature. It shows two methods for specifying a PRIMARY KEY for a 'unique_cats' table and demonstrates how to create an 'employees' table with an AUTO_INCREMENT primary key. Additionally, it includes a sample INSERT statement for adding a record to the employees table.

Uploaded by

rajsinghcool0708
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

L3 MySQL Primary Key, Auto - Increment and Excercise

The document provides examples of creating tables in SQL with a focus on defining primary keys and using the AUTO_INCREMENT feature. It shows two methods for specifying a PRIMARY KEY for a 'unique_cats' table and demonstrates how to create an 'employees' table with an AUTO_INCREMENT primary key. Additionally, it includes a sample INSERT statement for adding a record to the employees table.

Uploaded by

rajsinghcool0708
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Primary Key constraint

-- One way of specifying a PRIMARY KEY

​ CREATE TABLE unique_cats (


​ ​ cat_id INT PRIMARY KEY,
​ name VARCHAR(100) NOT NULL,
​ age INT NOT NULL
​ );
-- Another option:

​ CREATE TABLE unique_cats2 (


​ ​ cat_id INT,
​ name VARCHAR(100) NOT NULL,
​ age INT NOT NULL,
​ PRIMARY KEY (cat_id)
​ );
______________________________________________________________________

-- AUTO_INCREMENT

​ CREATE TABLE unique_cats3 (


​ cat_id INT AUTO_INCREMENT,
​ name VARCHAR(100) NOT NULL,
​ age INT NOT NULL,
​ PRIMARY KEY (cat_id)
​ );
_______________________________________________________________________
Exercise

Solution

-- Defining employees table

​ CREATE TABLE employees (


​ id INT AUTO_INCREMENT,
​ first_name VARCHAR(255) NOT NULL,
​ last_name VARCHAR(255) NOT NULL,
​ middle_name VARCHAR(255),
​ age INT NOT NULL,
​ current_status VARCHAR(255) NOT NULL DEFAULT 'employed',
​ PRIMARY KEY(id)
​ );

-- Another way of defining the primary key:

​ CREATE TABLE employees (


​ id INT AUTO_INCREMENT PRIMARY KEY,
​ first_name VARCHAR(255) NOT NULL,
​ last_name VARCHAR(255) NOT NULL,
​ middle_name VARCHAR(255),
​ age INT NOT NULL,
​ current_status VARCHAR(255) NOT NULL DEFAULT 'employed'
​ );

-- A test INSERT:

​ INSERT INTO employees(first_name, last_name, age) VALUES


​ ('Dora', 'Smith', 58);

You might also like