MorkaLork Development

Interesting stuff I've picked up over the years...

C#/MySql 4: Create table

2009-04-17 07:28:31 | 369 views | csharp class method tutorial mysql create table connection

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 =D

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;


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 =D

[italic]Read more about verbatim here: [url=http://www.morkalork.com/Reaper.php?id=37


Article comments

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.