Basic SQL - DELETE and CREATE
13 May 2009 @ 11:03AM

Updated: 25 Jan 2010 @ 01:06PM
Now comes the DELETE command. This is almost as important to know as INSERT. There are times when you need to delete certain rows from a table. When it comes down to it, the command is very similar to the UPDATE command.


A delete statement

Here I deleted our poor userid number 2. As you can see, 1 row was affected. As with the update, if you leave off the where clause, you delete the whole table. As an alternative to DELETE, if you're trying to delete the whole table, you can use the TRUNCATE TABLE command.


Truncate

This one doesn't show how many rows were deleted, but you probably don't really care if you're trying to delete all the rows. There is no undo. If you screw up and truncate or delete stuff you didn't want to, you better have a backup. Otherwise you are out of luck.
Comments (0)

Create a Table

The CREATE statement lets you create tables (and databases if you want). Above is an example. CREATE TABLE, as you may have guessed, tells SQL you're creating a table. Log is the name of the table. I then have a pair of parenthesis inside of which I define my columns. I have a column called logid, it's an integer, I set it to identity with a seed of 1 and an increment of 1 and I make it NOT NULL. I also create a userid column of type int not null. I create another column called action of type text. This one can be null. Then there's logDate which is a datetime field and not null. Finally, I have an ip field that's a 15 character long varchar field and set that to not null.

With that, we'll draw this tutorial to a close. You should now have a basic understanding of the most commonly used SQL commands. There are many, many more, but the ones detailed in this tutorial will be the ones you use over and over again. In a future tutorial we'll go over some more advanced SQL concepts, but this should give you a good start.
Comments (0)