Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
100% found this document useful (2 votes)
229 views

Create A Table: The Following Query Creates A Table

The document discusses SQL commands for creating databases and tables, inserting, updating, deleting and selecting data from tables. It covers CREATE, INSERT, UPDATE, DELETE and SELECT statements and provides examples of basic usage for each including creating a database and table, inserting rows, updating and deleting rows by column value, and selecting rows with optional WHERE clauses. It also discusses aggregate functions like SUM, AVG, COUNT etc.

Uploaded by

api-3718490
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
229 views

Create A Table: The Following Query Creates A Table

The document discusses SQL commands for creating databases and tables, inserting, updating, deleting and selecting data from tables. It covers CREATE, INSERT, UPDATE, DELETE and SELECT statements and provides examples of basic usage for each including creating a database and table, inserting rows, updating and deleting rows by column value, and selecting rows with optional WHERE clauses. It also discusses aggregate functions like SUM, AVG, COUNT etc.

Uploaded by

api-3718490
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

+http://www.roseindia.net/sql/where.

shtml

CREATE DATABASE database_name;

Create a Table: the following query creates a table


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

n 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)
)

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

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

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 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'

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