Creating a MySQL Table

Look at the reference manual for the complete syntax for creating a MySQL database.

The basic syntax for creating a table is

CREATE TABLE Employee (
  socsec       CHAR(9),
firstName VARCHAR(10),
lastName VARCHAR(15), birthDate DATE, salary DOUBLE, dependents INT,
PRIMARY KEY(socsec)
)

There are many field types, the most commonly used are explained below:

CHAR and VARCHAR: Use CHAR for fields that are an exact size such as state: CHAR[2]. Use VARCHAR when the size can vary, such as street: VARCHAR[25].
With CHAR the number in brackets is the exact size. With VARCHAR the number is the maximum size.

INT: A number with no decimal place.

DOUBLE: A number with decimal places.

Note: There are some fields that have the word 'number' in them that are not really numbers. If you will not do any arithmetic operations on a field, it is probably not numeric. Would you need to find the average phone number of all of your friends? Divide your social security number by 5? No! Of course not! Use CHAR or VARCHAR for fields that are not numbers.

DATE, TIME, and DATETIME: Date includes the month, day, and year. Time is on a 24 hour clock and include hours, minutes, seconds, and microseconds. DateTime combines both date and time.

AUTO_INCREMENT: If used, this must be the key.
INDEX, Field Types, Binary Dat, ALTER TABLE
Next lesson: Case Study: Checking AccountMAIN INDEX
EXAMPLES INDEX

Copyright © Zebra0.com
All rights reserved worldwide.

 
 

Field Types