C#/MySql 4: Create table
Written by:
maffelu
, 2009-04-17 07:28:31
To create a table in a database you need to know how to connect to a mysql server and how to create a database on that server. This means that the previous two sub articles for the C#/MySql article are mandatory
Now, this article assumes that you now know how to create a connection, so we won't repeat anything there, and that you also know how to ask and execute a
NonQuery command.
The new things here are actually just two things;
- The SQL command to create a table
- How to apply verbatim to make your command more readeable
First, the syntax to create a table is as follows:
CREATE TABLE tablename(COLNAME DATATYPE(VAL) [OPTIONS])
For example:
CREATE TABLE myfriend(name varchar(100), age int(3) NOT NULL)
This would create a table with two fields; One field called name which would be of the datatype varchar(string basically) and one field called age which would be of the datatype int which can't be null(according to our option parameter).
A list of datatype will be availeable soon as a subitem.
Now, when creating a table you might have many fields you want to enter, and entering them in one long row might be difficult to read and alter, so you ought to use the verbatim character to ease the reading.
In our example we're gonna create a table with eight(8) fields, so it would be a terribly long string if we didn't break it up.
Normal string:
commandLine = "CREATE TABLE MyContacts(name VARCHAR(150),address VARCHAR(255),zip VARCHAR(10),city VARCHAR(100),email VARCHAR(255),homephone VARCHAR(50),workphone VARCHAR(50),cellphone VARCHAR(50))";
With verbatim:
commandLine = @"CREATE TABLE MyContacts
(
name VARCHAR(150),
addresss VARCHAR(255),
)";
One of these are easier to read
[italic]Read more about verbatim here: [url=http://www.morkalork.com/Reaper.php?id=37
Sabinne
2011-11-23 00:36:58
Nice place! A "Search" function would be nice. I can't find something when I'm in a hurry.