[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
SqlCreateTable

(SQL) CREATE TABLE

Creates a table in a database. It's syntax is:-

CREATE TABLE tablename (
    field1    datatype[(length)] [NULL | NOT NULL] [auto_increment],
    field2    datatype[(length)] [NULL | NOT NULL] [DEFAULT 'value'],
    field3    datatype[(length)] [NULL | NOT NULL] [DEFAULT 'value'],
    primary key (field1)
)


Where tablename is the name of the table you want to create. The details of the table are then placed between the brackets. Note that you do not need to put line breaks in like I have as eacy field is delimited by the comma, but it makes it look a lot neater.

To define a field, you first put the name of the field. You then specify it's datatype (e.g. INT, VARCHAR, ENUM). Length is pretty obvious apart from if you are defining an enumeration, in which case you put the possible values in the brackets seperated by commas, e.g. ('Y', 'N'). You can then specify whether the field is allowed to be empty, and assign it a default value.

The auto_increment is a special parameter and you should only have one of thse per table as a rule of thumb. When a new record is added, this number is incremented, meaning each record that goes into the table gets its own unique number. A field that is unique to every record is known as a primay key, and we thus define it as such after all the fields have been defined.

TO DO: Add notes on defining indexes and some actual examples of table creation queries.

create table People
(
  LastName  varchar,
  FirstName varchar,
  Address   varchar,
  Age       int
)


last edited (March 21, 2007) by bilderbikkel, Number of views: 4383, Current Rev: 7 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.