Databases - Creating Structure
9 May 2008 @ 09:20AM

Updated: 25 Jan 2010 @ 09:22AM
You should have a view of the main interface. The left has your tree of information while the right has further info about whatever it is you're highlighting on the left. This isn't meant to be a SQL server tutorial so I'll keep the instructions and explanations brief. On the left, expand Databases. You should have System Databases and that's it. Right click on Databases and choose "New Database...". In the dialog, give the database a name. I'll call mine Guestbook. You can call it pretty much anything you want. Click ok and you should now have a Guestbook database listed in your databases list. Go ahead and expand it, then expand tables.

Creating Our Database

Here you'll see System Tables and that's about it. Right click on tables and choose, you guessed it, "New Table...". This is going to be the database table that we store our data in. On the right you'll see the properties window. For (Name) give the table a name. I called mine 'comments'. In the center area, we're going to create our actual table schema. A schema is basically a table layout. Here's what mine looks like.

The Table Schema

Ok, here's what I have. The first column (highlighted) is called commentid. It is set to be an int (integer). If you look at the column properties underneath, you'll see it's set as identity with an identity increment of 1 and an identity seed of 1. This means that this column will automatically increment itself every time a new row is added. We do this so we have some unique way of accessing every single row in this table. Since the value is incrementing itself, it has to be some sort of number, like int (what we chose), bigint, numeric, or something else. You can't set a non-integer type field to be an identity. If you're not sure what I'm talking about, just nod and smile and remember to set a column as an identity in any tables you build. You'll thank me eventually. Be sure to hit the plus next to "Identity Specification" and set "Is Identity". You can't set "Identity Specification" directly.

Next we have a column called name of type varchar(50). This means any text can be entered into this column, up to 50 characters. I'm not expecting anyone to have a name longer than 50 characters. Then we have the comment column. This will hold the comment (duh). It's set to text which has no size limit. Following that we have commentDate. This is a datetime field that holds, yes, the date and time. That way when someone makes a comment, we can store precisely when the comment was made. Lastly we have a column called IP. This will hold the ip address of the person making the comment. Thus if some guy keeps making stupid comments, you can find his IP address and block him or something. This column is varchar(15) since an ip address can't be long than 15 characters right now. This'll be true unless IPv6 sees widespread adoption. If it ever does, we'd need to change this to 39 characters.

Save your table (ctrl-s) and there you have it, our database and table are set up. Once again, this wasn't meant as a SQL Server tutorial so I glossed over everything pretty quickly. Our focus is using C# to put data into this table and take it out, not so much the table itself. With that in mind, let's go back to C# and do some magic.
Comments (0)