Go Back   Webmaster Forum > Development > Database and SQL
REMOVE the ads below !
Reply
 
LinkBack Thread Tools
  # 1 (permalink)
Old
Max
Regular Geek
Posts: 338
Join Date: Feb 2007
iTrader: (0)
[003] Mysql - Create a table - 01-03-2008

Create a table

Type:
USE us_presidents;
then press ENTER

The USE command allows you to start using the database us_presidents.

Displaying text
Sometimes a string of commands is too wide to fit on the pages of this book. In those cases, just continue typing in the same line.
For instance, this command:
rpm -i MySQL-3.23.51-1.i386.rpm MySQL-client-3.23.51-1.i386.rpm
could be displayed this way:
rpm -i MySQL-3.23.51-1.i386.rpm
MySQL-client-3.23.51-1.i386.rpm
If the text on your terminal screen wraps to the next line, just keep typing—it'll work fine.

Type: CREATE TABLE name (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, first CHAR(25), last CHAR(25)); then press ENTER. The window should look like this:

This string of commands is used to CREATE a TABLE called name with three fields: id, first, and last. CREATE TABLE name (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, first CHAR(25), last CHAR(25));

Here are the datatypes and properties for these fields:
  • INT
    CREATE TABLE name (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, first CHAR(25), last CHAR(25) );
    The INT datatype for the id field ensures it will contain only integers—numbers, not text.
  • NOT NULL
    CREATE TABLE name (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, first CHAR(25), last CHAR(25) );
    The NOT NULL property ensures the id field cannot be left blank.
  • PRIMARY KEY
    CREATE TABLE name (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, first CHAR(25), last CHAR(25) );
    The PRIMARY KEY property makes id the key field in the table.
    In any database table, one field should be the key field—a field that can contain no duplicates. In this table, name, the id field is the key field because it contains the PRIMARY KEY property.
    This means the name table can't have two records with an id of 35.
  • AUTO_INCREMENT
    CREATE TABLE name (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, first CHAR(25), last CHAR(25) );
    The AUTO_INCREMENT property automatically assigns a value to the id field, increasing the previous id number by one for each new field.
    This ensures that the NOT NULL (can't be blank) and the PRIMARY KEY (can't have duplicates) properties of the id field are both satisfied.
  • CHAR
    CREATE TABLE name (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, first CHAR(25), last CHAR(25) );
    The CHAR datatype for the first and last fields limits the length of entries to 25 characters each.
In the us_presidents database, you've created a table called name that's organized like this:
Field Datatype Properties
id NT primary key, not null, auto increment
first CHAR(25)
last CHAR(25)


Yours Sincerely,
Max.
Reply With Quote
Reply


Thread Tools



PSD to HTML

vBulletin®, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd. | SEO by vBSEO | Skin developed by vBStyles.com