C#/MySql 4: Create table
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])
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.
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))";
commandLine = @"CREATE TABLE MyContacts
One of these are easier to read
[italic]Read more about verbatim here: [url=http://www.morkalork.com/Reaper.php?id=37
Feel free to comment this article using a facebook profile.
I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.