PDA

View Full Version : [SQL]Tutorial.



Cocain
27-03-10, 11:05 AM
////////////SQL Introduction.////////////

Although SQL is an ANSI standard, there are many different versions of the SQL language.
However, to be compliant with the ANSI standard, they all support at least the major commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner.

Note: Most of the SQL database programs also have their own proprietary extensions in addition to the SQL standard. However, the above posted commands, are quite nice, and come in handy, when working with a WorldOfWarcraft database, for any emulator.<

Another note : Standing the other way of Lua, SQL isn't case sensitive, you should keep that in mind.

////////////The Start Of The Guide////////////
If you know this SQL commands, you're quite far.

* SELECT - extracts data from a database
* UPDATE - You can update your data with this commands in your database.
* DELETE - You can delete unwished collumns with this command from your database.
* INSERT INTO - You can insert new data in your database, with this command.
& then standing at the side of this

* CREATE DATABASE - creates a new database
* CREATE TABLE - Name says it itself.
* ALTER TABLE - Alter, means modify, so you can simply modify a table with this command.
* DROP TABLE - Delete a table with this command.

So, we'll start with the acctual guide now.
Your first SQL statement.

The following SQL statement will select all the records in the "Items" table:

SELECT * FROM Items

Now, that you selected the records, from the Items table, you can also select a certain collumn out of it. We'll still be working in the 'Items' database.

SQL SELECT Syntax

SELECT Name (This is the collumn name.)
FROM Items (Table name.)

and

SELECT * FROM Items (Table name.)

Now, you want to select the content of the columns named Names, Itemid from the table 'Items.' This can be done this way :


SELECT Names,Itemid(Collumn Names) FROM Items(Table Name)

Next, selecting all collumns, from the table 'Items.' You can do this on a quite simple way. The * in SQL stands for selecting all the collumns.
So :


Select * From Items.

The next part. Using Distincts. Hence I'm not so good at them yet, I took them from a small guide, posted on a private forum.

In a table, some of the columns may contain duplicate values. This is not a problem, however, sometimes you will want to list only the different (distinct) values in a table.

The DISTINCT keyword can be used to return only distinct (different) values.


SELECT DISTINCT column_name(s)
FROM table_name

Now we want to select only the distinct values from the column named "City" from the table above.

We use the following SELECT statement:


SELECT DISTINCT City FROM Persons

End of the copied part.
Now, we'll move on to the next part of my personal guide.

Using the 'Where' command. When you select somthing, with the 'Where' command, you select collumns, with specified vallues.

Example.


SELECT Names (Collumn name.
FROM Items (Table name.)
WHERE (Your specification here.)

We'll use the WHERE command now, to select all collumns in the table 'Items' where it contains a 'Lol' in the name.


SELECT * FROM Items (Selects all collumns from the table items.)
WHERE Name='Lol' (Speaks for itself.)

When you're using 1,2,3,.. numbers, so, you arn't needed to use ' at the begin, and to close it at the end. When you're using text values, you're supposed to open, and close it.


This is correct:

SELECT * FROM Items WHERE Name='Lol'

This is wrong:

SELECT * FROM Persons WHERE Name=Lol

The AND operator displays a record if both the first condition and the second condition is true.

The OR operator displays a record if either the first condition or the second condition is true.

So :

Select * FROM Items
Where Name = 'Lol' (Name, both are collumns.)
AND Id ='4500' (The Itemid.)

So no need to apply a second 'Where.' when you're using an AND command.

Next, the use of the 'OR.' command :


Select * FROM Items
Where Name ='Lol'
OR Name='Lol1'.

Q:
A:Yes you can.
On this way :


SELECT * FROM ITEMS
WHERE Name='Lol'
AND (Itemid='4500' OR Itemid='4501')


Congrats, you now can 'look up' collumns, and tables, select them, from the database.

////////////INSERTING////////////

The INSERT INTO statement is used to insert a new row in a table.


INSERT INTO Items
VALUES (value1, value2, value3,...)

The value insert of 1 - 2 - 3 has to be the structure of your database.

for example.


INSERT INTO Items
VALUES (4500(Id),'Loller(Name)', '25484(Displayid)')

This will add the data into all collumns, now we're going to add data into specific collumns :


INSERT INTO Persons (Id, Name)
VALUES (4500, 'Loller')

In this case, your inserted item doesn't have a displayid.

Now we're going to update the collumns.


UPDATE Items
SET Names=Lollerlol, Itemid=4500
WHERE Names=Lol AND Itemid=45000


Deleting collumns. <3


DELETE FROM Items
WHERE Itemid=4500


Can it be more easyer?


DELETE FROM Items
WHERE Name='Lol' AND Name='Loller'

This is it so far. Hope you enjoyed it.

Synapse
01-04-10, 12:09 PM
Thanks for the guide. I needed to grasp some SQL basics :)

Just one question please: Are indentations allowed in SQL queries?

Thanks

Cocain
01-04-10, 12:34 PM
Thanks for the guide. I needed to grasp some SQL basics :)

Just one question please: Are indentations allowed in SQL queries?

Thanks


I don't see how you're even able to use them in SQL queries. But I wouldn't advise trying it. :P

Synapse
03-04-10, 03:52 AM
Ok, thanks for the tip.

Pedregon
18-04-10, 05:54 AM
Very useful. Stickied.

ghost15
19-04-10, 01:13 AM
Very nice, i will use this for sure. Im sick of doing the long ways of finding scripts that already have it set up for me :P

erik_wolff
04-06-10, 10:31 PM
This will come in quite handy. Thanks for the info.

thunderqt
30-06-10, 08:55 AM
thanks, +rep