SQL for Beginners
SQL is a standard language for accessing and manipulating databases. SQL is an abbreviation for Structured Query Language. Earlier, SQL was known as SEQUEL. SQL allows you to create simple as well as very complex database queries that are fired or executed against databases to deal with data present in databases. Dealing might be retrieving values from databases, inserting values into databases, modifying data, etc. To store any sort of data in databases, firstly, you need to create tables and declare its fields. It is mandatory to declare atleast one field in order to store values under that field.
There are several commands in SQL which are required to deal with tables created in databases and with its values. These are,
- INSERT INTO
These are very basic commands. As soon as we log in into database, first we need to create a database. Here, database is not the database like SQL. Here, creating a new database means allocating or creating part of database by giving name as desired. New database can be created by using following SQL query.
CREATE DATABASE UserDatabase
After writing above query, just select the whole query and execute. As this query gets executed, a database named "UserDatabase" will be created in your database (MySQL or Oracle). Then next step is to activate user created database. For that, You need to execute following query.
USE DATABASE UserDatabase
After executing this query, database named UserDatabase will get activated. And now, you can proceed by creating tables, inserting values in it.
To create a table, you need to decide the name for it and this name should match the requirements of your problem. This name should not be weird. It should be meaningful and always a noun. Also, you need to decide fields in the tables and these names must be meaningful. Here, table "Orders" is created with fields as Order_Id, OrderName. Data types for these fields should be same as required.
CREATE TABLE Orders ( Order_Id int, OrderName varchar (255) )
After executing above query your table Orders with fields Order_Id and OrderName will be created. Now, next step is to insert values or records in table. In SQL, each complete row with values for all of the fields is known as a Record.
INSERT INTO Orders VALUES ( 1, 'Dispatch')
After executing above query, one record with values as 1 and Dispatch will be created in table Orders. Similarly, you can use above query with different values to insert records into table.
Now, to see whether values are properly inserted into table or not, following query needs to be used.
SELECT * FROM Orders
After execution of above query, all records will get displayed. If you want to see the record of one particular record, following query needs to be used.
SELECT * from Orders WHERE Order_Id = 1
Here, WHERE is a clause which is used at the time of condition. When above query is executed, whole record with Order_Id as "1" is only displayed.
If you want to see values of only one field, then use following query,
SELECT Order_Id from Orders
After executing above query, values under Order_Id field will only be displayed. Similarly, we can modify any of the records present in the table. For this, we need to use following query.
UPDATE Orders SET Order_Id = 2 WHERE OrderName = 'Dispatch'
After executing this query, record with OrderName as "Dispatch" gets modified or updated with Order_Id as "2". Now, if you wish to delete any one particular record from a table then DELETE command needs to be used and that too, in the following manner.
DELETE FROM Orders where Order_Id = 1
With this query, you will be able to delete record from your table. Now, finally, if you wish to delete table as a whole, you don't need to use DELETE command but you will be required to use DROP command.
DROP TABLE Orders
After execution of above query, table with name as "Orders" will be deleted. similarly, you can use DROP command to delete database as well.
DROP DATABASE UserDatabase
So, in this way, you can deal with database and its data by using above listed queries.
Like it on Facebook, +1 on Google, Tweet it or share this article on other bookmarking websites.