# updated dec 2020# Creates a Simple User table# Uses an auto-incrementing primary key as userId CREATETABLEuser(
userId INTNOTNULLAUTO_INCREMENTPRIMARYKEY,
username VARCHAR(100),
password VARCHAR(100))ENGINE=InnoDB;
-- 'CREATE TABLE' followed by the name of the table. -- In round brackets, define the columns.CREATETABLE`test_table`(
id INT(10)PRIMARYKEY,
username VARCHAR(50)NOTNULL);
//Create database table mysql// Note for id always set it to auto-increment (AI)CREATETABLE`database_name`.`new_table_name`(`id`INTNOTNULLAUTO_INCREMENT,`first_name`VARCHAR(45)NOTNULL,`last_name`VARCHAR(45)NOTNULL,`title`VARCHAR(45)NOTNULL,PRIMARYKEY(`id`,`first_name`,`last_name`,`title`));
Imaginons que l’ont souhaite créer une table utilisateur,
contenant "id","nom","prenom","email","date_naiss","pays" etc..
La requête pour créer cette table peut ressembler à ceci:
CREATETABLE utilisateur
(
id INTPRIMARYKEYNOTNULL,
nom VARCHAR(100),
prenom VARCHAR(100),
email VARCHAR(255),
date_naissance DATE,
pays VARCHAR(255),
ville VARCHAR(255),
code_postal VARCHAR(5),
nombre_achat INT)
Voici des explications sur les colonnes créées :
id : identifiant unique qui est utilisé comme clé primaire
et qui n’est pas nulle
nom :une colonne de typeVARCHAR avec un maximum de 100 caractères
prenom : idem mais pour le prénom
email : adresse email enregistré sous 255 caractères au maximum
date_naissance : format AAAA-MM-JJ (exemple : 1973-11-17)
pays : nom du pays 255 caractères au maximum
ville : idem pour la ville
code_postal : 5 caractères du code postal
nombre_achat : nombre d’achat de cet utilisateur sur le site
#Assuming that there is a database called MyDatabase#creates a table called TableName, with columns Name, Age, and ClassUse MyDatabase;createtable TableName(No.intprimarykey,
Name varchar(50),
Age int,
Class varchar(15));
The CREATETABLE statement allows you tocreate a new tablein a database.
The following illustrates the basic syntax of the CREATETABLE statement:
CREATETABLE[IFNOTEXISTS] table_name(
column_1_definition,
column_2_definition,...,
table_constraints
)ENGINE=storage_engine;
Let’s examine the syntax in greater detail.First, you specify the name of the table that you want tocreateafter the CREATETABLE keywords. The table name must be uniquewithin a database. The IFNOTEXISTSis optional. It allows you tocheckif the table that you create already existsin the database.If this is the case, MySQL will ignore the whole statement and will notcreateany new table.Second, you specify a list ofcolumnsof the tablein the column_list section,columns are separated by commas.
Third, you can optionally specify the storage enginefor the tablein the ENGINE clause. You can useany storage engine such asInnoDBand MyISAM.If you don’t explicitly declare a storage engine, MySQL will useInnoDBbydefault.