All you do is take, take, take. It's not right. The database has emotions too. It has needs. Face it, if your tawdry love affair with the employee database is ever gonna blossom into a real, meaningful relationship (and what man doesn't want that?), then you are going to have to learn how to give a little. Unless you are Chuck Norris. Chuck Norris doesn't give -- he takes.
To insert a new row into a table, you'll need the ever helpful INSERT INTO statement.
INSERT INTO EMPLOYEE
VALUES ('BRUCE', 'LEE', 'SECURITY', $50,000);
This would result in the following row being added to the table:
Note: now that Bruce Lee is added to your Employee list, you do not want to make him mad.
If you ever want to insert a new row of data, but for some reason do not have enough information to fill in every column, the INSERT INTO statement can help with that as well.
INSERT INTO EMPLOYEE (FirstName, LastName)
VALUES ('Bruce', 'Lee');
The above code would insert the first and last name into the table and leave the remaining columns blank, to be filled in when you obtain the remaining information.
The Update Statement
As discussed above, we never put in the rest of Bruce Lee's information, which means good ole' Bruce isn't getting paid this week. And he is mad. Fighting mad, which is really the worst kind of mad for Bruce Lee to be. But never fear, use your fingers of fury to pound out the following code.
UPDATE EMPLOYEE SET SALARY=$50,000
That's right. I just saved your life. Because now the row will contain Bruce's salary.
But now you've forgotten to add the Position, so Bruce is just walking around being a slacker. If ninjas attack he won't know what to do. Should he fight them off or do their taxes? Never fear. You can update multiple columns with the UPDATE statement.
SET SALARY=$50,000, POSITION='Security'
Lastly, the UPDATE command also allows us to insert a whole new column.
The DELETE Statement
Eventually someone is going to get fired. Or murdered by Bruce Lee (let's face it, the guy is a loose canon). Then it will be time to pull out the old DELETE statement.
DELETE from EMPLOYEE WHERE FirstName='Homer';
This will delete the row containing Homer Sampsons information.
Now in the event that Homer was Bruce Lee's victim, we will have to delete the entire database, due to the impending lawsuits. Here is how we do that:
DELETE * FROM EMPLOYEE;
You can also Delete a column from your table.
DELETE FROM EMPLOYEE
And that's it for the basics of SQL. Get a little rest and work on that kung-fu grip. The second tutorial in the Beginning SQL series will be coming soon.
blog comments powered by Disqus