By Alvin Alexander. Last updated: May 1, 2019
Just a quick note here today that if you need some MySQL `create table` examples, I hope these are helpful. I created them for some experiments I ran last night. They show the MySQL create table
, primary key
, and foreign key
syntax:
create table authors ( id int auto_increment not null primary key, first_name varchar(20), last_name varchar(20) ); create table books ( id int auto_increment not null primary key, title varchar(32) ); create table book_authors ( id int auto_increment not null primary key, book_id int not null, author_id int not null, foreign key (author_id) references authors(id), foreign key (book_id) references books(id) ); insert into authors (first_name, last_name) values ('Alan', 'Watts'); insert into authors (first_name, last_name) values ('Shunryu', 'Suzuki'); insert into authors (first_name, last_name) values ('Robert', 'Aitken'); insert into authors (first_name, last_name) values ('Carlos', 'Castaneda'); insert into books (title) values ('Zen Master Raven'); insert into books (title) values ('Zen Mind, Beginner\'s Mind'); insert into books (title) values ('The Way of Zen'); insert into books (title) values ('The Teachings of Don Juan'); -- there is a better way to do this insert into book_authors (book_id, author_id) values (1, 3); insert into book_authors (book_id, author_id) values (2, 2); insert into book_authors (book_id, author_id) values (3, 1); insert into book_authors (book_id, author_id) values (4, 4);
For a very large MySQL syntax example, see my MySQL database design for Nagios. It shows many examples of creating tables and fields, drop table
commands, constraints, and more.