Download Document 7760007

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Transcript
Creating a Table
Creating a Table
The SQL commands we've encountered so far have been reasonably simple, but
as tables are so flexible, it takes a more complicated command to create them.
The basic form of the command is as follows:
mysql>CREATE TABLE table_name (
-> column_1_name column_1_type column_1_details,
-> column_2_name column_2_type column_2_details,
-> ...
->);
Let's return to our example Jokes table. Recall that it had three columns: ID (a
number), JokeText (the text of the joke), and JokeDate (the date the joke was
entered). The command to create this table looks like this:
mysql>CREATE TABLE Jokes (
-> ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> JokeText TEXT,
-> JokeDate DATE NOT NULL
->);
It looks pretty scary, huh? Let's break it down:
❑ The first line is fairly simple: it says that we want to create a new table called
Jokes.
❑ The second line says that we want a column called ID that will contain an
integer (INT), that is, a whole number. The rest of this line deals with special
details for this column. First, this column is not allowed to be left blank (NOT
NULL). Next, if we don't specify any value in particular when we add a new
entry to the table, we want MySQL to pick a value that is one more than the
highest value in the table so far (AUTO_INCREMENT). Finally, this column is to
act as a unique identifier for the entries in this table, so all values in this
column must be unique (PRIMARY KEY).
❑ The third line is super-simple; it says that we want a column called JokeText,
which will contain text (TEXT).
❑ The fourth line defines our last column, called JokeDate, which will contain
data of type DATE, and which cannot be left blank (NOT NULL).
Note that, while you're free to type your SQL commands in upper or lower case,
a MySQL server running on a UNIX-based system will be case-sensitive when it
41
Document related concepts

Object-relational impedance mismatch wikipedia, lookup

Microsoft SQL Server wikipedia, lookup

Clusterpoint wikipedia, lookup

Open Database Connectivity wikipedia, lookup

Relational model wikipedia, lookup

Database model wikipedia, lookup