Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Iqra Technology SQL Training Day 6-1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

SQL Training: Day 06

Iqra Technology (www.iqratechnology.com)


By Shaikh Javed
In this session we will learn:

 Insert Into
 Null values
 Update
 Delete
Insert Into
Insert Into:

SQL INSERT STATEMENT


 SQL INSERT statement is a SQL query. It is used to insert a single or a multiple records in a table.
 There are two ways to insert data in a table:

 By SQL insert into statement


 By specifying column names
 Without specifying column names

 By SQL insert into select statement


1) Inserting data directly into a table
You can insert a row in the table by using SQL INSERT INTO command.

There are two ways to insert values in a table.

In the first method there is no need to specify the column name where the data will be inserted, you need only
their values.

INSERT INTO table_name


VALUES (value1, value2, value3....);
Insert Into:

 Let's take an example of table which has five records within it.  
INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)
VALUES (1, ABHIRAM, 22, ALLAHABAD);
INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)
VALUES (2, ALKA, 20, GHAZIABAD);
INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)
VALUES (3, DISHA, 21, VARANASI);
INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)
VALUES (4, ESHA, 21, DELHI);
INSERT INTO STUDENTS (ROLL_NO, NAME, AGE, CITY)
VALUES (5, MANMEET, 23, JALANDHAR);
It will show the following table as the final result.
Insert Into:

 You can create a record in CUSTOMERS table by using this syntax also.
INSERT INTO CUSTOMERS   
VALUES (6, PRATIK, 24, KANPUR);  

The following table will be as follow:


Inserting data through SELECT Statement:

SQL INSERT INTO SELECT Syntax.

INSERT INTO table_name


[(column1, column2, .... column)]
SELECT column1, column2, .... Column N
FROM table_name [WHERE condition];
Null values
Null values:

SQL NULL Values
What is a NULL Value?
 A field with a NULL value is a field with no value.

 If a field in a table is optional, it is possible to insert a new record or update a record without adding a value to this field. Then,
the field will be saved with a NULL value.

 Note: A NULL value is different from a zero value or a field that contains spaces. A field with a NULL value is one that has been
left blank during record creation!
How to Test for NULL Values?
It is not possible to test for NULL values with comparison operators, such as =, <, or <>.

We will have to use the IS NULL and IS NOT NULL operators instead.
IS NULL Syntax

SELECT column_names
FROM table_name
WHERE column_name IS NULL;
Null values:

IS NOT NULL Syntax

SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;

The IS NULL Operator

The IS NULL operator is used to test for empty values (NULL values).
The following SQL lists all customers with a NULL value in the "Address" field:

SELECT CustomerName, ContactName, Address


FROM Customers
WHERE Address IS NULL;

 
Null values:

The IS NOT NULL Operator

The IS NOT NULL operator is used to test for non-empty values (NOT NULL values).
The following SQL lists all customers with a value in the "Address" field:

SELECT CustomerName, ContactName, Address


FROM Customers
WHERE Address IS NOT NULL;

 
Update
Update:

SQL UPDATE
 The SQL commands (UPDATE and DELETE) are used to modify the data that is already in the database. The SQL DELETE
command uses a WHERE clause.
 SQL UPDATE statement is used to change the data of the records held by tables. Which rows is to be update, it is decided by a
condition. To specify condition, we use WHERE clause.
 The UPDATE statement can be written in following form:

UPDATE table_name SET [column_name1= value1,... column_nameN = valueN] [WHERE condition]

Let's see the Syntax:

UPDATE table_name
SET column_name = expression
WHERE conditions

Let's take an example: here we are going to update an entry in the source table.

 
Update:

SQL statement:

UPDATE students
SET User_Name = 'beinghuman'
WHERE Student_Id = '3'

 
Update:

Updating Multiple Fields:

If you are going to update multiple fields, you should separate each field assignment with a comma.

SQL UPDATE statement for multiple fields:

UPDATE students
SET User_Name = 'beserious', First_Name = 'Johnny'
WHERE Student_Id = '3‘
Delete
Delete :

SQL DELETE

The SQL DELETE statement is used to delete rows from a table. Generally DELETE statement removes one or more records from a
table.
SQL DELETE Syntax
Let's see the Syntax for the SQL DELETE statement:

DELETE FROM table_name [WHERE condition];


Here table_name is the table which has to be deleted. The WHERE clause in SQL DELETE statement is optional here.

 
Delete :

Example of delete with WHERE clause is given below:

DELETE FROM EMPLOYEE WHERE ID=101;

 
Delete :

 Resulting table after the query:

DELETE FROM EMPLOYEE;  
Delete :

 It will delete all the records of EMPLOYEE table.

 It will delete the all the records of EMPLOYEE table where ID is 101.

 The WHERE clause in the SQL DELETE statement is optional and it identifies the rows in the column that gets deleted.

 WHERE clause is used to prevent the deletion of all the rows in the table, If you don't use the WHERE clause you might loss all
the rows.

 Invalid DELETE Statement for ORACLE database


 You cannot use * (asterisk) symbol to delete all the records.

 DELETE * FROM EMPLOYEE;

 
Q&A Session
Thanks.

You might also like