SQL Example
SQL Example
1. Data Normalization
In this section we will learn about the concepts of data normalization. The most
important thing in database designing is to make sure that the data get properly
distributed among the tables.
2. Create a database
we can create a database by using the Database name. In SQL the key word
'Show databases' finds the existing databases. You may create your own or use
an existing one after that create connection with database.
3. The INSERT INTO Statement
To insert records into a table, use the key word insert into 'table name', just write
a list of column names separated by commas, followed by a closing parenthesis
then using keyword values, write the list of values enclosed in parenthesis.
4. The UPDATE Statement
The UPDATE statement is used to modify the data in the database table in a
specified manner. In the syntax of update statement the keyword 'set' is used to
assign a new value to a selected column. The statement is accomplished by a
where clause.
5. The DELETE Statement
The DELETE statement is used to delete rows in a table. database will update that
is why deletion and insertion of data will be done.
6. The SELECT Statement
SELECT key word is used to select data from a table. The tabular result is stored
in a result set. SELECT column_name give the path FROM table_name.
SQL-introduction
SQL stands for Structured Query Language. It is easy and allows you to create a
database. It is an ANSI language which allows an user to write queries to access, modify
and delete the customize data. SQL consist of data definition language (DDL) and data
manipulation language (DML).
Data Definition Language : It is used to create, alter, drop or delete new database tables.
You can also create indexes and even make a link between tables. The selected keywords
which are used to perform the particular task are as under:
Data Manipulation Language : It is used to getting, insert, delete and update data in
database and tables. The selected keywords which are used to perform the particular task
are as under:
Data Normalization
In this section you will learn the concepts of data normalization. The most important
thing in database designing is to make sure that the data get properly distributed among
the tables. Simply we can say that the designing of the table in proper manner is called
Normalization.
Normalization is a process that is used in relational database design to organize the data
for minimizing the duplication. In normalization, we divide the database in two or more
tables and create a relationship between them. After isolating the data we perform some
addition, deletion or modification on the fields of a table then we propagate and remove
the duplicate data from the related tables.
Data integrity
To make optimized queries on the normalized tables and produce fast,
efficient results.
To make faster index and also make a perfect sorting.
To increase the performance of the database.
Create a database
First of all, display all databases by show databases. This helps to find out the existing
database. Then create a database using the given syntax and give the suitable name of
database.
Then create connection with database. The connection is included in the names of the
database.
The INSERT INTO statement is used to insert or add a record of data into the table.
To insert records into a table, just write the key word INSERT INTO 'table name', and
write a list of column names separated by commas, followed by a closing parenthesis
then use keyword values, write the list of values enclosed in parenthesis. One more thing
we should remember always that strings should be enclosed in single quotes, and
numbers should not.
Syntax:
To insert a new row in a database table we have to write the given below code:
employee:
Let us consider we want to insert data in field name 'emp_name', 'Position' and in
'email_id' with there specific values then we should use the following SQL statement:
The UPDATE statement is used to modify the data in the database table through a
specified criteria. In the given syntax of update statement the keyword SET is used to
assign a new value to a selected columns. The statement also uses WHERE clause.
It simply work as a select statement, if the given criteria match with the table content then
the selected row will be effected if not then the whole table will be effected.
If we want to change the salary to the employee with a emp_name of "Amar" then we
should use the following SQL statement :
Syntax:
for example:
UPDATE Person SET Salary = 10000
WHERE emp_name = 'Amar';
UPDATE employee
SET email_id = 'amar@newsindia.com', Position = 'Programmer'
WHERE emp_name = 'Amar';
The DELETE statement is used to delete rows from a table. database will update that is
why deletion and insertion of data will be done.
Syntax
DELETE FROM table_name
WHERE column_name = some_value
Person:
LName FName Address
ram Raj delhi-5
sonu Shiv delhi-5
To Delete a Row :
Result
To Delete All the Rows : This means that the table structure, attributes, and indexes will
be drop.
DELETE FROM table_name
Syntax:
SELECT column_name(s)
FROM table_name
Example: To select the content of columns named "LName" and "FName", from the
database table called "email", use a SELECT .
Select All Columns: To select all columns from the "email" table, use a * symbol
instead of column names.
WHERE clause is used with the SELECT keyword. ' Where' is a clause which is used for
searching the data with a particular condition.
If the data in the table matches condition then it returns the specific value of that
particular data.
Syntax
Operator Description
= Equal
BETWEEN Between an inclusive range
LIKE
Search for a pattern
IN If you know the exact value you want to
return for at least one of the columns
WHERE in SQL:
Aggregate Functions
In this section we are going to illustrate aggregate function, with the help of which we
can use many arithmetic operation like average, count, maximum and many more in
SQL. They are briefly described and denoted by the keyword in the given below section.
AVG
COUNT
MAX
MIN
SUM
For all of the above given function the standard code syntax will be:
For example we just take a Employee table and use the aggregate function SUM on the
field "Salary" to find out the total salary of the Employee table.
Table Employee:
To find out the total salary of the company's employee we should write the following
code:
When we run the above query we will get the following result:
SUM(Salary)
55000