Start Learning Basic Database Programming Commands

in #utopian-io8 years ago (edited)

MySQL programming is at the heart of today's web applications. Learn the basic 4 commands SELECT, UPDATE, INSERT and DELETE.

What Will I Learn?

  • Basic MySQL Database Programming Commands

Requirements

  • MySQL Server

Difficulty

  • Intermediate

Tutorial Contents

How to use SELECT, UPDATE, INSERT and DELETE commands.

Start Learning Basic Database Programming Commands

This tutorial covers concepts and syntax of some basic SQL commands. In order to begin working with MySQL Server and try out the examples listed here, install the free MySQL platform applications.

There are 4 basic SQL commands that will be discussed. While each of these commands have depth and complexity that are beyond the scope of this primer, they have their basic forms and understanding them is an excellent place to begin.

How to Use MySQL SELECT Command

Perhaps the most commonly used SQL command of all, SELECT is used to view records in the database. Open SQL Server and a new query window. Type the following command then click "Execute" or equivalently type Ctrl-e.

SELECT *
FROM Person.Contact



The results are displayed below the query windows. In the lower right corner it shows how long the query took and how many records were retrieved. There are 19,972 records in the Person.Contact table. In the command above, the "" indicates a selection of all fields, or columns, in the table. To limit these, replace the "" with a list of the columns desired.

SELECT Title, FirstName, LastName, EmailAddress
FROM Person.Contact



View the result and see that only the four columns listed are now included in the results. To view only the contacts that have a title of "Sr." use the WHERE clause to specify a limitation on the type of records returned. This yields 11 records.

SELECT Title, FirstName, LastName, EmailAddress
FROM Person.Contact
WHERE Title = 'Sr.'



Notice that the form [column] = [value] is used by the WHERE clause to limit the records returned to only those that match the specified value in the specified field. The use of single-quotes is important to specify string values, as double-quotes have a different meaning in MySQL.

How to Use MySQL UPDATE Command

The UPDATE command is used to modify 1 or more records in a table. Use the WHERE clause to limit which records are updated, just as it was used to limit which records were returned for the SELECT command. View a record in the Person.Contact table, then update it and view it again.

SELECT *
FROM Person.Contact
WHERE ContactID = 10
UPDATE Person.Contact
SET FirstName = 'Ron'
WHERE ContactID = 10
SELECT *
FROM Person.Contact
WHERE ContactID = 10



Notice that the contact's first name has been changed from "Ronald" to "Ron." It is extremely important that the WHERE clause is used with UPDATE. If it were removed from the UPDATE statement above, every single contact in the Person.Contact table would now have the first name "Ron" because there was nothing to limit the scope of the update. Without a limit, or conditional, SQL will happily update every record.

How to Use MySQL INSERT Command

The INSERT command creates a new record in a table. View the records in the Production.Location table, add a new record to it, then view it again. The SELECT statements used to view the records are left to the reader as an exercise.

INSERT 
INTO Production.Location (Name, CostRate, Availability)
VALUES ('Plastics', 14.5, 90.0)



The list of columns after the table name specify that values will be provided for Name, CostRate and Availability. The other two columns in the table, LocationID and ModifiedDate are automatically set by SQL so values are not provided for them. Notice that the number of columns listed must match the number of values provided.

How to Use MySQL DELETE Command

Finally, the DELETE command, as expected, deletes one or more records from a table. The same caution must be taken with DELETE as with UPDATE. If no WHERE clause is specified, every single record in the table will be deleted! Delete the "Plastics" record that was just inserted into Production.Location.

DELETE
FROM Production.Location
WHERE LocationID = 61



Practice variations of these four commands on the AdventureWorks database. Don't be afraid to make mistakes, as a fresh copy of the database can always be installed.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

I accept this post entrusting that it's a introductory post to a whole curriculum of posts teaching MySQL. Hopefully you continue posting these, moving from basic to advanced.

You can contact us on Discord.
[utopian-moderator]

Hey @evariste I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.082
BTC 62352.20
ETH 1625.45
USDT 1.00
SBD 0.44