0% found this document useful (0 votes)
59 views11 pages

SQL Example

The INSERT INTO statement is used to insert records into a database table. It requires the table name and a list of column names and values to insert. Values for string columns should be enclosed in single quotes. The statement inserts the new record into the table.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
59 views11 pages

SQL Example

The INSERT INTO statement is used to insert records into a database table. It requires the table name and a list of column names and values to insert. Values for string columns should be enclosed in single quotes. The statement inserts the new record into the table.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 11

SQL Example, Codes and Tutorials

                          

SQL: A brief Introduction


SQL stands for Structured Query Language. It is easy and allows us 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).

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.
         

7. The WHERE Clause


WHERE clause is used with the SELECT keyword. 'Where' is a clause which is
used for searching the data with a particular condition. 
8. The 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.

                          

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:

CREATE TABLE - creates a new table

ALTER TABLE - alters a database table

DROP TABLE - deletes a database table

CREATE INDEX - creates an index (search key)

DROP INDEX - deletes an index

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:

SELECT - displays data from table

UPDATE - updates data in table

DELETE - deletes data from table

INSERT INTO - inserts new data into table

                          
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. 

The main goals of normalization process are: 


- To eliminate the redundancy of data 
- To make sure that the data dependencies (relationship) make sense. 
By these two goals we reduce the space occupied by the duplicate data in the database
tables and ensure that the data is logically stored there.

Some of the positive points of the data normalization in database is as under :

 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.

First normal Form (1NF)


The First Normal Form requires the atomic values in each column. Atomic means the set
of values are not available with in the column. In other words, in First Normal Form
table must have at least one candidate key and make sure that the table don’t have
any duplicate record. In First Normal Form repeating groups are not allowed, that is no
attributes which occur a different number of times on different records.

Second normal Form (2NF)


The Second Normal Form can be achieved only when a table is in the 1NF. We can make
the 2NF by eliminating the partial dependencies. As the First Normal Form deals with
only the atomicity of data, but Second Normal Form deals with the relationships of tables
like composite keys and non-key columns. In Second Normal Form subset of data is
removed and is organized in separate tables. This process is applied to multiple rows of a
table till the duplicity get reduced.
   
Third Normal Form (3NF)
The Third Normal Form can be achieved only when a table is in the Second Normal
Form. We can make the 3NF by eliminating all transitive dependencies lying among the
fields of a record. In Third Normal Form, all columns should depend on the primary key
only i.e. remove the column, which is not dependent upon the primary key.
    
Boyce-Codd Normal Form (BCNF)
In the normalization Boyce - Codd Normal Form needs a table to meet the Third Normal
Form. In Boyce - Codd Normal Form every non-trivial functional dependency must be a
dependent on a superkey.
     
Fourth Normal Form (4NF)
The Fourth Normal Form can be achieved when a table meets the Boyce-Codd Normal
Form. Fourth Normal Form confirms that the independent multivalued facts are
efficiently and correctly represented in database design.

Fifth Normal Form (5NF)


The Fifth Normal Form is achieved only when a table meets the Fourth Normal Form. In
this normalization it is ensured that all non-trivial join dependencies get eliminated.

                          

Create a database
                          

In this section, we will teach you to 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.

CREATE DATABASE database_name;

Then create connection with database. The connection  is included in the names of the
database. 

Create a Table: the following query creates a table


CREATE TABLE table_name
(
column_name1 data_type,
.......
)

Yet the table does not have any data. 


In the given example we show the use of the create to make a table email and populate it
with the field named LName, FName and email_add.

CREATE TABLE email


(
LName varchar(20),
FName varchar(20),
email_add varchar(40)
)

                          

The INSERT INTO Statement


                          

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:

INSERT INTO 'table_name'('field_name', 'field_name'. . .)


VALUES ('field_value', 'field_value'. . .);

Insert a New Row

To insert a new row in a database table we have to write the given below code:

INSERT INTO employee 


VALUES ('Amar', 'Designer', 10000, 'amar@roseindia.com');

The output of the above code will be:

employee:

emp_name Position Salary email_id


Amar Designer 10000 amar@roseindia.com

Insert Data in Specified Columns


Let us consider we have a table named employee which have the following records:

emp_name Position Salary email_id


Amar Designer 10000 amar@roseindia.com

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:

INSERT INTO employee (emp_name, Position, email_id)


VALUES ('Vinod', 'Programmer', 'vinod@roseindia.com');

The output is like:

emp_name Position Salary email_id


Amar Designer 10000 amar@roseindia.com
Vinod Programmer   vinod@roseindia.com

                          

The UPDATE 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.

Lets consider a record with a emp_name of "Amar" as shown below.

emp_name Position Salary email_id


Amar Designer 8000 amar@roseindia.com

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:

UPDATE 'table_name' SET 'field_name' = 'new_value'


WHERE 'field_name' = 'field_value';

for example: 
UPDATE Person SET Salary = 10000
WHERE emp_name = 'Amar';

The output of the above code will be :

emp_name Position Salary email_id


Amar Designer 10000 amar@roseindia.com

To Update several Columns in a Row: If we want to change the multiple values of a


table like in employee table we want to change Position and email_id then we have to
write the following code in which we set the email_id and position by SET keyword and
putting condition by keyword WHERE for emp_name is 'Amar'.

UPDATE employee
SET email_id = 'amar@newsindia.com', Position = 'Programmer'
WHERE emp_name = 'Amar';

The output of the above code will be:

emp_name Position Salary email_id


Amar Programmer 10000 amar@newsindia.com

                          

The DELETE Statement


                          

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

Let's consider a table :

Person:
LName FName Address
ram Raj delhi-5
sonu Shiv delhi-5

To Delete a Row  : 

Suppose the row with Lname="sonu" is needed to get deleted


.
DELETE FROM Person WHERE LName = 'sonu'

Result

LName FName Address


ram Raj delhi-5

To Delete All the Rows : This means that the table structure, attributes, and indexes will
be drop.
DELETE FROM table_name

                          

The SELECT statement for SQL


                          

SELECT key word is used to select data from a table. 

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   Name, Father_Name FROM EMAIL

The database table "email":


Name Father_Name  Address
Ram Sonu  delhi-5

  Select All Columns: To select all columns from the "email" table, use a * symbol
instead of column names.

SELECT * FROM email

                          

WHERE Clause in SQL 


                          

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

SELECT column FROM table


WHERE column operator value

 The following operators can be used:

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:

 we add a WHERE  to the SELECT statement: 

SELECT * FROM Persons


WHERE unit='india'
 
                          

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:

SELECT "function type"


("column_name") FROM "table_name"

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:

emp_Name Salery Joining_Date


Amit 15000 Feb-05-2005
Chandan 25000 Feb-17-2005
Ravi 8000 Feb-25-2005
Vinod 7000 Feb-28-2005

To find out the total salary of the company's employee we should write the following
code:

SELECT SUM (Salary) FROM Employee;

When we run the above query we will get the following result:
SUM(Salary)
55000

                          

You might also like