Showing posts with label select statement in sql. Show all posts
Showing posts with label select statement in sql. Show all posts

Friday, 7 September 2012

SQL Insert


Inserting a new row into the dept table

Insert into dept (deptno, dname, loc)
values (10,'Apple','Boston')

You can omit the column list in the insert statement but then you have to enter those in the same order as they appear in the table and you have o include all the columns in the values.

The following example illustrates the use of the "default" keyword while inserting the records.

insert into table_name values(default);
or
insert into table_name(column1,column2..) values(default) 

How to create a new table having the same structure as some other table?

create table new_table
as
select *
from old_table

How to insert data into multiple tables?

There are two terms used INSERT ALL AND INSERT FIRST

EXAMPLE:
Suppose there are three tables emp, emp_1 and emp_2 with the same structure and columns.

insert all
when sal in (800, 1600) then
into emp (empno, ename,job) values (empno, ename,job)
when sal = 3200 then
into emp_1 (empno, ename, job) values (empno,ename,job)
else
into emp_2 (empno, ename,job) values (empno,ename,job)
select empno, ename, job
from emp

INSERT FIRST will breakout as soon as it sees a condition that evaluates to true.
INSERT ALL will evaluate al the conditions even if the previous condition evluates to true.

Insert Default Values

Some RDBMSs, including SQL Server, allow you to create a new row based on default values that are defined in the table specification using the DEFAULT VALUES method in place of the VALUES section. Suppose you have a table like this:

CREATE TABLE A (
A_ID int IDENTITY PRIMARY KEY,
Name varchar (100) DEFAULT 'Anonymous',
Dates varchar (20) DEFAULT 'Unknown);

And you if want to insert a new row with just the default values for each column and the auto-generated ID field. You can’t do it using the standard INSERT...VALUES syntax, so instead you'd use this:

INSERT INTO A DEFAULT VALUES;

And that does the trick. Using DEFAULT VALUES instead of the standard VALUES clause inserts the default value for every column (or an auto-generated value for identity columns). If there isn't a default value for a non-identity column, the value is treated as NULL. If any column in the table is NOT NULL, isn't an identity column, and doesn't have a default value, then an error will be raised.

Performing a Multi-Row INSERT

INSERT INTO S_Backup SELECT EveryName, EveryCost FROM S;
Alternatively, you can easily reshape data and rename columns of rows using a statement such as this:
   
INSERT INTO S_1 (TheName, TheCost)
SELECT EveryName, EveryCost FROM S;

SQL tuning advisor

Purpose


This tutorial shows you how to use the SQL Tuning Advisor feature in Oracle SQL Developer 3.0.

Complete time

Approximately 20 minutes.

Overview

The SQL Tuning Advisor analyzes high-volume SQL statements and offers tuning recommendations. It takes one or more SQL statements as an input and invokes the Automatic Tuning Optimizer to perform SQL tuning on the statements. It can run against any given SQL statement. The SQL Tuning Advisor provides advice in the form of precise SQL actions for tuning the SQL statements along with their expected performance benefits. The recommendation or advice provided relates to the collection of statistics on objects, creation of new indexes, restructuring of the SQL statement, or creation of a SQL profile. You can choose to accept the recommendation to complete the tuning of the SQL statements.

Oracle Database can automatically tune SQL statements by identifying problematic SQL statements and implementing tuning recommendations using the SQL Tuning Advisor. You can also run the SQL Tuning Advisor selectively on a single or a set of SQL statements that have been identified as problematic.

In this tutorial, you learn how to run and review the recommendations of the SQL Tuning Advisor.
Note: Tuning Advisor is part of the Tuning Pack, one of the Oracle management packs and is available for purchase with Enterprise Edition. For more information see The Oracle Technology Network or the online documentation
  
Software and Hardware Requirements 

The following is a list of software requirements:
  • Oracle Database 11g Enterprise Edition with access to the Tuning and Diagnostic management packs and with the sample schema installed. 
  • Oracle SQL Developer 3.0.  

Prerequisites

Before starting this tutorial, you should: 
1 .
Install Oracle SQL Developer 3.0 from OTN. Follow the readme instructions here.
2 .
Install Oracle Database 11g with the Sample schema.

Creating a Database Connection

The first step to managing database objects using Oracle SQL Developer 3.0 is to create a database connection.

Perform the following steps to create a database connection:
Note: If you already have database connections for SCOTT and SYSTEM, you do not need to perform the following steps. You can move to Providing Privileges to the Scott User topic.



  1. If you have installed the SQL Developer icon on your desktop, click the icon to start your SQL Developer and move to Step 4. If you do not have the icon located on your desktop, perform the following steps to create a shortcut to launch SQL Developer 3.0 from your desktop.
Open the directory where the SQL Developer 3.0 is located,right-click             sqldeveloper.exe(on Windows) or sqldeveloper.sh (on Linux) and select Send to > Desktop (create shortcut)

  1. On the desktop, you will find an icon named Shortcut to sqldeveloper.exe. Double-click the icon to open SQL Developer 3.0.
        Note: To rename it, select the icon and then press F2 and enter a new name.