MySQL simple CREATE TABLE example

Im in the process of developing a small database for a client and thought I would start some simple tutorials before I get into anything more adventuresome.

Before we start we need a platform, this can be via PHP or Java query request or by simply using the MySQL console.



 CREATE DATABASE smallbiz;

 CONNECT  smallbiz; /*We must connect to our created database before we continue*/


/*create table with auto increment customer id and set it as the primary key 
, then 2 field that hold some data first name and last name and set then as varchar
with a maximum of 15 characters 
 */


CREATE TABLE Customers
(
CustID int NOT NULL AUTO_INCREMENT,
 PRIMARY KEY(CustID),
 FirstName varchar(15),
 LastName varchar(15)
);



/*similar thing here a auto increment ID and a varchar for the title of the product. */


CREATE TABLE Products
(
 ProdID int NOT NULL AUTO_INCREMENT,
 PRIMARY KEY(ProdID),
 Title varchar(15)
);

/*This table is the orders table, some call this the XBOX,  this table also has a primary key auto
increment, but also have references to the primary keys in the other tables,
these are called foreign keys in this table. */

CREATE TABLE Orders
(
 OrdID int NOT NULL AUTO_INCREMENT,
 PRIMARY KEY(OrdID),

 OCustID int,
 OProdID int,
 FOREIGN KEY (OCustID) REFERENCES Customers(CustID),
 FOREIGN KEY (OProdID) REFERENCES Products(ProdID)
);


No comments:

Post a Comment