[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
SqlCreateTable
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.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
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 )
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
