mysql create table primary key code example

Example 1: alter table add foreign key mysql

ALTER TABLE orders
ADD 
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE;

Example 2: create table in mysql

# Creates a Simple User table
# Uses an auto-incrementing primary key as userId 

CREATE TABLE user (
    userId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(100),
    password VARCHAR(100) 
) ENGINE=InnoDB;

Example 3: mysql change primary key

ALTER TABLE tableName MODIFY COLUMN id INT; /* First you should drop auto increment */
ALTER TABLE tableName DROP PRIMARY KEY; /* Dop primary key */
ALTER TABLE tableName ADD PRIMARY KEY (new_id); /* Set primary key to the new column */
ALTER TABLE tableName MODIFY COLUMN new_id INT AUTO_INCREMENT; /* Set auto increment to the new primary key */

Example 4: create table mysql query

create table tutorials_tbl(
   tutorial_id INT NOT NULL AUTO_INCREMENT,
   tutorial_title VARCHAR(100) NOT NULL,
   tutorial_author VARCHAR(40) NOT NULL,
   submission_date DATE,
   PRIMARY KEY ( tutorial_id )
);

Example 5: mysql create table

CREATE TABLE nom_de_la_table
(
    colonne1 type_donnees,
    colonne2 type_donnees,
    colonne3 type_donnees,
    colonne4 type_donnees
)

Tags:

Sql Example