Friday, 18 August 2017

SQL query to UPDATE table in the DATABASE

The UPDATE Statement

When we need to modify existing row, we need to use the UPDATE statement. If needed, we can update multiple records at the same time. For the UPDATE to be successful the user must have data manipulation privileges (UPDATE privilege) on the table or column and the updated value must not conflict with all the applicable constraints (such as primary keys, unique indexes, CHECK constraints, and NOT NULL constraints).

Syntax

UPDATE table_name
SET column_name = value, column_name = value...
WHERE condition;

table_name
It is the table name
SET
Modification clause
column_name
Existing column name which is to be modified.
value
New value to be updated to the column or subquery for the column.
condition
Basis on which the modification is performed. It can include column names, expressions, constants, subqueries, and comparison operators

Example

Let us see the output row from students table where the rollno is 4.

select * from students
where rollno=4;






We will update the lastname for the student whose roll no is 4 with use of update statement example as shown below.

update students
set lastname='Green'
where rollno=4;

commit;

select * from students
where rollno=4;





We can see that the Oliver’s lastname has been updated from Queen to Green.

We must prefer the primary key to identify a single row. Using other columns may unexpectedly cause several rows to be updated. If we omit the WHERE clause, ALL records will be updated.

update students
set lastname='Green';
commit;

The above query will update all the rows in students table and set lastname to Green.

select * from students;











So the UPDATE statement should be used with caution.


Updating Multiple Columns with a Subquery

It is possible to update multiple columns in the single query. Also there is provision to take the value which has to be set as an output from the sub query. A subquery is a query that is nested inside a SELECT, INSERT, UPDATE, or DELETE statement, or inside another subquery. A subquery can be used anywhere an expression is allowed. A subquery is also called an inner query or inner select, while the statement containing a subquery is also called an outer query or outer select. The SELECT query of a subquery is always enclosed in parentheses. 

Syntax

UPDATE table_name
SET column_name = (SELECT column_name FROM table_name WHERE condition),
      column_name = (SELECT column_name FROM table_name WHERE condition)
WHERE condition;

Example

Let us update students table using subquery on the same students table as shown below. Here we are updating firstname and city column for row with roll no 45.

update students
set lastname=(select firstname from students where rollno=14),
      CITY=(select CITY from students where rollno=21)
where rollno=45;

commit;

The lastname column in the outer query is populated by subquery which refers to firstname column of students where roll no is 14.

The city column the outer query is populated by subquery which refers to city column of students where roll no is 21.

Before update the output of students table is as shown below.

select * from students;









After update the output of students table is as shown below.

select * from students;









Updating Rows Based on another Table

We can update multiple columns as well as from multiple sub queries and also refer to multiple tables.

Syntax

UPDATE table_name
SET column_name = (SELECT column_name FROM table_name_1 WHERE condition),
      column_name = (SELECT column_name FROM table_name_2 WHERE condition)
WHERE condition;

Example

Now we will update students_copy table by taking values  from students table.

select * from students_copy; 









Below is an update statement where the lastname for Barry is Allen is updated to Kara. The lastname column in the outer query is populated by subquery which refers to firstname column of students where roll no is 14.

update students_copy
set lastname=(select firstname from students where rollno=14)
where rollno=45;

commit;

Let us check the output after update.

select * from students_copy; 


Monday, 14 August 2017

How to create a table from existing table in database

We can create duplicate of table currently present in the database with the utilization of Data Definition language and Data modification language. CREATE and SELECT statements are used, Additionally INSERT statement is used to copy table into existing table. The new table has same columns. We can choose some specific columns or all columns. At the point when the new table is made with the old table, the new table will have the values of the old table.

Syntax

create table new_table_name as select * from old_table_name;

Copying table structure only and not the table data


We need to just use a where clause that won't select any rows:

create table new_table_name as select * from old_table_name where 1=2;

Example
Let us create copy of students table structure  in students_copy

create table students_copy as select * from students where 1=2;

select * from students;











select * from students_copy;








We can see that only the structure is copied, no rows are present in students_copy table as present in students table. To avoid iterate again and again and insert nothing based on the condition where 1=2; we can execute below SQL statements:

create table students_copy as select * from xyz where rownum = -1;
create table students_copy as select * from students where rownum = 0;
create table students_copy as select * from students where rownum <1;

Inserting rows from a table into another existing table using SELECT statement

 

INSERT INTO new_table_name (column,..)
SELECT (column,..) FROM old_table_name;

Example

Insert into students_copy select * from students;

select * from students_copy;










Creating new table form multiple tables


CREATE TABLE new_table_name
  AS (SELECT column_1, column2, ... column_n
      FROM old_table_name, old_table_name_2, .. old_table_name_n where condition);

Example

Let us first have a look at students and marks table for reference:

select * from marks











select * from students;










create table persons as select firstname,lastname,rollno,english from students,marks;











We received above error since both the students and marks table have rollno column in common so we need to specify the table name in prefix to refer. Also if we don’t mention the where clause then it will create 6*6=36 rows in persons table. Modified query is as shown below:

create table persons as select students.firstname,students.lastname,students.rollno,marks.english from students,marks
where students.rollno=marks.rollno;

select * from persons;


Thursday, 10 August 2017

How to INSERT row into table

The INSERT statement is one of the syntax of DML DATA MANIPULATION LANGUAGE. The INSERT statement is used to insert a single record or multiple records into a table in Oracle. When inserting records into a table, value should be provided for every not null column. Also we can avoid a column from the INSERT statement if the column allows NULL values.

Syntax
INSERT INTO table [(column, column...)]
VALUES (value, value...);

Example
Let us take students table for reference.

select * from students
As we can see currently the table is empty and not a single row present.
Now we will insert one record into the students table by below SQL query.

INSERT INTO students (FirstName, LastName, RollNo, Address, City)
VALUES ('Rachel','Green','21','A4 central perk','Ottawa');

COMMIT;

select * from students
We can see row for Rachel Green has been inserted in the students table.

INSERT ALL SQL Statement
The INSERT ALL statement is used to add multiple rows with a single INSERT statement. The rows can be inserted into one table or multiple tables using only one SQL command.

Example

We will now insert 5 records into students table using INSERT ALL statement as shown below:

INSERT ALL
into students ( firstname, lastname , rollno, address, city ) 
values ('Oliver','Queen',04,'1501P Avenue','Starling')
into students ( firstname, lastname , rollno, address, city ) 
values ('Barry','Allen',45,'701 Madison Building','Central')
into students ( firstname, lastname , rollno, address, city ) 
values ('Kara','Danvers',14,'108 Times Square','National')
into students ( firstname, lastname , rollno, address, city ) 
values ('Leonard','Snart',12,'201 B Prospect Plaza','Central')
into students ( firstname, lastname , rollno, address, city ) 
values ('Bruce','Wayne',13,'Star Mansion','Gotham')
select * from dual;

COMMIT;

select * from students
All the records are successfully inserted in the students table.