Create A Table in MySQL

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Create A Table In MySQL

Create New Table

For this example we'll create two tables. The first one describe the species of animals available in a pet store and the
second will store the data of a pet in the store. The table names will be species and pet

The species table will consist of the id and the animal species, and the pet table will consist of animal id, species, sex,
and price

mysql> CREATE TABLE species (id INT NOT NULL AUTO_INCREMENT, species varchar(30)
NOT NULL, primary key(id));
Query OK, 0 rows affected (0.02 sec)

mysql> CREATE TABLE pet(id INT NOT NULL AUTO_INCREMENT, sp_id INT NOT NULL, sex
CHAR(1) NOT NULL, price DECIMAL(4,2) NOT NULL, primary key(id));
Query OK, 0 rows affected (0.03 sec)

mysql> SHOW tables;


+--------------------+
| Tables_in_petstore |
+--------------------+
| pet |
| species |
+--------------------+
2 rows in set (0.00 sec)

mysql> DESCRIBE species;


+---------+-------------+----+-----+---------+---------------+
| Field | Type |Null| Key | Default | Extra |
+---------+-------------+----+-----+---------+---------------+
| id | int(11) | | PRI | NULL | auto_increment|
| name | varchar(30) | | | | |
+---------+-------------+----+-----+---------+---------------+
2 rows in set (0.05 sec)

mysql> DESC pet;


+-------+--------------+----+-----+---------+----------------+
| Field | Type |Null| Key | Default | Extra |
+-------+--------------+----+-----+---------+----------------+
| id | int(11) | | PRI | NULL | auto_increment |
| sp_id | int(11) | | | 0 | |
| sex | char(1) | | | | |
| price | decimal(4,2) | | | 0.00 | |
+-------+--------------+----+-----+---------+----------------+
4 rows in set (0.00 sec)

http://www.php-mysql-tutorial.com/mysql-tutorial/create-mysql-table.php (1 of 3)12/12/2006 12:14:23 PM


Create A Table In MySQL

The SQL sytax to create table is : CREATE TABLE <tablename> (<list of fields>)

The DESCRIBE or DESC statement is used to show a description of a table. You can also use EXPLAIN or SHOW
COLUMNS

mysql> EXPLAIN pet;


+-------+--------------+----+-----+---------+----------------+
| Field | Type |Null| Key | Default | Extra |
+-------+--------------+----+-----+---------+----------------+
| id | int(11) | | PRI | NULL | auto_increment |
| sp_id | int(11) | | | 0 | |
| sex | char(1) | | | | |
| price | decimal(4,2) | | | 0.00 | |
+-------+--------------+----+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> SHOW COLUMNS FROM pet;


+-------+--------------+----+-----+---------+----------------+
| Field | Type |Null| Key | Default | Extra |
+-------+--------------+----+-----+---------+----------------+
| id | int(11) | | PRI | NULL | auto_increment |
| sp_id | int(11) | | | 0 | |
| sex | char(1) | | | | |
| price | decimal(4,2) | | | 0.00 | |
+-------+--------------+----+-----+---------+----------------+
4 rows in set (0.00 sec)

PHP MySQL Tutorial : Create


Create New MySQL Database Insert Data To MySQL
Table In MySQL

Search

www.php-mysql-tutorial.
Web com

This tutorial is far from perfect so if you have any critiques, questions, comments or problems about this tutorial
please tell me. Click here to send your feedback. And if you like this tutorial please link to this site. It will really help a
lot :-)

http://www.php-mysql-tutorial.com/mysql-tutorial/create-mysql-table.php (2 of 3)12/12/2006 12:14:23 PM


Create A Table In MySQL

My Experimental Sites

1. Crocodile Hunter DVD 2. Kids & Baby Apparel Store 3. DVD Players, Home Theater Systems 4. Action Figures, Kids
Activities & Learning 5. Baby Play Centers & Walkers

Copyright © 2004 - 2006 www.php-mysql-tutorial.com

http://www.php-mysql-tutorial.com/mysql-tutorial/create-mysql-table.php (3 of 3)12/12/2006 12:14:23 PM

You might also like