SlideShare una empresa de Scribd logo
1 de 32
Descargar para leer sin conexión
www.atnyla.com
RummanAnsari
About SQL Tutorial
SQL stands for Structured Query Language. The SQL language as a
query language, it can do much more than just query a database. It can
define the structure of the data, modify data in the database, and specify
security constraints.
A database system provides a data definition language to specify the
database schema and a data manipulation language to express database
queries and updates. In practice, the data definition and data manipulation
languages are not two separate languages; instead they simply form parts of a
single database language, such as the widely used SQL language.
About This Tutorial
This SQL tutorial has been prepared for the beginners to help them
understand the basic to advanced concepts of SQL. This tutorial will give you
enough understanding on the various components of SQL along with suitable
examples.
Prerequisites
Before you start practicing with various types of examples given in this
tutorial, we are assuming that you are already aware about what a
database is, especially the RDBMS and what is a computer
programming language.
Audience
Students who are pursuing BCA, MCA, B.E/B.Tech
(CS/IT/EC/EE/ME/Civil/Any other discipline)
Students who have "Java Programming with SQL" in their curriculum
Candidates who wish to switch from service based companies to product based
Candidates who want to continue their career as JAVA developer with SQL
Candidates who are passionate about coding
©Privacy
Contact information: atnyla (http://www.atnyla.com).
By: Rumman Ansari (http://www.atnyla.com/tuition).
 View 11
SQL Basic Overview
What is SQL
SQL stands for Structured Query Language and can be pronounced as
SQL or sequel – (Structured Query Language) . Defined, SQL is a query
language used for accessing and modifying information in one or more data
tables and rows of a database.
SQL DataBase
In a simple manner, SQL is a non-procedural, English-like language that
processes data in groups of records rather than one record at a time.
Few SQL functions of are:
Store data
Modify data
Retrieve data
Delete data
Create tables and other database objects
A Brief History of SQL
SQL Database Design
IBM first developed SQL in 1970s. Also it is an ANSI/ISO standard. It has
become a Standard Universal Language used by most of the relational
database management systems (RDBMS). Some of the RDBMS systems are:
Oracle, Microsoft SQL server, Sybase etc.
 View 9
1970 − Dr. Edgar F. "Ted" Codd of IBM is known as the father of
relational databases. He described a relational model for databases.
1974 − Structured Query Language appeared.
1978 − IBM worked to develop Codd's ideas and released a product
named System/R.
1986 − IBM developed the first prototype of relational database and
standardized by ANSI. The first relational database was released by
Relational Software which later came to be known as Oracle.
Most of these have provided their own implementation extensions, thus
enhancing their RDBMS system features and making it a powerful tool.
These RDBMS systems, all use the popular SQL commands SELECT, UPDATE,
DELETE, INSERT, WHERE in similar format.
SQL Database Table
SQL database is constructed of a number of tables. In a business,
SQL tables would be used to divide and simplify the different areas of the
operation: Table for Customers, one for Vendors, Employees and so on.
SQL Database Table Columns
Each SQL table is made up of a number of columns, referred to as
fields and run along the top of the table. Sql columns or fields have their
content (object/data/info) defined into character types; such as text, date,
numeric, integer, length to name a few.
SQL Database Table Rows
Each SQL table row, referred to a record, is located in the left
column of the table. Sql record row will contain a string of data containing
data matching up to each column field across the top. So, in a Customer
table each customer record would consist of one row with data for the
customer ID number, customer name, address, phone ... email and so on.
Simple Overview of various SQL query
SQL Query Types
SELECT Statement Retrieve records from a table
SELECT LIMIT Statement Retrieve records from a table and limit results
SELECT TOP Statement Retrieve records from a table and limit results
INSERT Statement Insert records into a table
UPDATE Statement Update records in a table
DELETE Statement Delete records from a table
TRUNCATE TABLE
Statement
Delete all records from a table (no rollback)
UNION Operator Combine 2 result sets (removes duplicates)
UNION ALL Operator Combine 2 result sets (includes duplicates)
INTERSECT Operator Intersection of 2 result sets
MINUS Operator Result set of one minus the result set of
another
EXCEPT Operator Result set of one minus the result set of
another
SQL Comparison Operators
Comparison Operators Operators such as =, <>, !=, >, <, and so on
SQL Joins
JOIN Tables Inner and Outer joins
SQL Aliases
ALIASES Create a temporary name for a column or table
SQL Clauses
DISTINCT Clause Retrieve unique records
FROM Clause List tables and join information
WHERE Clause Filter results
ORDER BY Clause Sort query results
GROUP BY Clause Group by one or more columns
HAVING Clause Restrict the groups of returned rows
SQL Functions
COUNT Function Return the count of an expression
SUM Function Return the sum of an expression
MIN Function Return the min of an expression
MAX Function Return the max of an expression
AVG Function Return the average of an expression
SQL Conditions
AND Condition 2 or more conditions to be met
OR Condition Any one of the conditions are met
AND & OR Combining AND and OR conditions
LIKE Condition Use wildcards in a WHERE clause
IN Condition Alternative to multiple OR conditions
NOT Condition Negate a condition
IS NULL Condition Test for NULL value
IS NOT NULL Condition Test for NOT NULL value
BETWEEN Condition Retrieve within a range (inclusive)
EXISTS Condition Condition is met if subquery returns at least one row
SQL Tables and Views
CREATE TABLE Create a table
CREATE TABLE
AS
Create a table from another table's definition and data
ALTER TABLE Add, modify or delete columns in a table; rename a table
DROP TABLE Delete a table
GLOBAL TEMP
Tables
Tables that are distinct within SQL session
LOCAL TEMP
Tables
Tables that are distinct within modules and embedded
SQL program
SQL VIEW Virtual tables (views of other tables)
©Privacy
Contact information: atnyla (http://www.atnyla.com).
By: Rumman Ansari (http://www.atnyla.com/tuition).
Query Language DDL, DML, DCL
Data-definition language (DDL): The SQL DDL provides commands for
defining relation schemas, deleting relations, and modifying relation schemas.
DDL - Data Definition Language
Command & Description
CREATE
Creates a new table, a view of a table, or other objother
objectsatabase.
ALTER
Modifies an existing database object, such as a table.
DROP
Deletes an entire table, a view of a table or other objects in the
database.
Interactive data-manipulation language (DML): The SQL DML includes a
query language based on both the relational algebra and the tuple relational
calculus. It includes also commands to insert tuples into, delete tuples from, and
modify tuples in the database.
DML - Data Manipulation Language
Command & Description
SELECT
Retrieves certain records from one or more tables.
INSERT
Creates a record.
UPDATE
Modifies records.
 View 9
DELETE
Deletes records.
View definition: The SQL DDL includes commands for defining views.
Transaction control. SQL includes commands for specifying the beginning and
ending of transactions.
Embedded SQL and dynamic SQL. Embedded and dynamic SQL define how SQL
statements can be embedded within general-purpose programming languages,
such as C, C++, Java, PL/I, Cobol, Pascal, and Fortran.
Integrity. The SQL DDL includes commands for specifying integrity constraints
that the data stored in the database must satisfy. Updates that violate integrity
constraints are disallowed.
Authorization. The SQL DDL includes commands for specifying access rights to
relations and views.
DCL - Data Control Language
Command & Description
GRANT
Gives a privilege to user.
REVOKE
Takes back privileges granted from user.
©Privacy
Contact information: atnyla (http://www.atnyla.com).
By: Rumman Ansari (http://www.atnyla.com/tuition).
SQL Plus
SQL*Plus is the most basic Oracle Database utility, with a basic command-
line interface, commonly used by users, administrators, and programmers.
Command types
SQL*Plus understands five categories of text:
1. SQL statements
2. PL/SQL blocks
3. SQL*Plus internal commands, for example:
environment control commands such as SET
environment monitoring commands such as SHOW
4. Comments
5. External commands prefixed by the ! char
Scripts can include all of these components.
An Oracle programmer in the appropriately configured software
environment can launch SQL*Plus, for example, by entering:
$ sqlplus scott/tiger
where the Oracle user scott has the password tiger . SQL*Plus then
presents a prompt with the default form of:
SQL>
Interactive use can then start by entering a SQL statement (terminated by a
semicolon), a PL/SQL block, or another command. For example:
SQL> select 'Hello world' as example from dual;
EXAMPLE
--------------------------------
Hello world
©Privacy
Contact information: atnyla (http://www.atnyla.com).
By: Rumman Ansari (http://www.atnyla.com/tuition).
 View 6
Connect with a system user in SQL
Create a new user inside Oracle Database
After installation of Oracle software. We have to create a user inside it. In this
tutorial, we are going to show you how to create a new user. So let's start.
To create a new user. Start SQL PLUS form your windows menu. It flows the
below statements for SQL 11g release 2 version.
For this, you have to login in the system user for the highest privilege. First I
will tell you how to Connect to the Oracle Database from SQL*Plus.
To connect to Oracle Database from SQL*Plus: (Using
SQL PLUS)
After typing the username and password it will look like this. By default, the
username is system and password in that password which you gave at the
time of installation, in my case the password is alone#i#4
 View 6
To connect to Oracle Database from SQL*Plus: (Using
CMD)
1. If you are on a Windows system, display a Windows command prompt.
2. At the command prompt, type sqlplus and press the key Enter.
SQL*Plus starts and prompts you for your username.
3. Type your username and press the key Enter.
SQL*Plus prompts you for your password.
4. Type your password and press the key Enter.
For security, your password is not visible on your screen.
The system connects you to an Oracle Database instance.
You are in the SQL*Plus environment. At the SQL> prompt, you can enter
and run SQL*Plus commands, SQL statements, PL/SQL statements, and
operating system commands.
To exit SQL*Plus, type exit and press the key Enter.
Exiting SQL*Plus ends the SQL*Plus session, but does not shut down
the Oracle Database instance.
SQL*Plus: Release 11.2.0.1.0 Production on Wed Jul 18 22:30:24 2018
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Enter user-name: system
Enter password: alone#i#4 (For security, your password is not visible on your
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL>
How to see the recent database user
SQL> SHOW user;
USER is "SYSTEM"
SQL>
©Privacy
Contact information: atnyla (http://www.atnyla.com).
By: Rumman Ansari (http://www.atnyla.com/tuition).
Microsoft Windows [Version 10.0.10240]
(c) 2015 Microsoft Corporation. All rights reserved.
C:Usersuser1>SQLPLUS
SQL*Plus: Release 11.2.0.1.0 Production on Wed Jul 18 22:24:33 2018
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Enter user-name: system
Enter password: alone#i#4 (For security, your password is not visible on your
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL>
Create a Database User in SQL
As always, begin by connecting to your server where Oracle is hosted, then
connect to Oracle itself as the SYSTEM account.
The SYSTEM account is one of a handful of predefined administrative
accounts generated automatically when Oracle is installed. SYSTEM is capable
of most administrative tasks, but the task we’re particularly interested in is
account management.
Creating a User
Once connected as SYSTEM , simply issue the CREATE USER command to
generate a new account.
Run the below query to check whether you are in SYSTEM user or not
If you don't know how to connect with SYSTEM user please see the previous
tutorial
SQL> SHOW user;
USER is "SYSTEM"
SQL>
CREATE USER user_name IDENTIFIED BY MyPassword;
Example
Here I am creating one user. In my case user name is Username: atnylaUser
and password is: rumman_ansari
SQL> CREATE USER atnylaUser IDENTIFIED BY rumman_ansari;
User created.
SQL>
Here we’re simply creating a atnylaUser account that is IDENTIFIED or
authenticated by the specified rumman_ansari .
The Grant Statement
 View 7
With our user_name new account created, we can now begin adding
privileges to the account using the GRANT statement. GRANT is a very
powerful statement with many possible options, but the core functionality is
to manage the privileges of both users and roles throughout the database.
Providing Roles
Typically, you’ll first want to assign privileges to the user through attaching
the account to various roles, starting with the CONNECT role:
GRANT CONNECT TO user_name;
Example
SQL> GRANT CONNECT TO atnylaUser;
Grant succeeded.
SQL>
In some cases to create a more powerful user, you may also consider adding
the RESOURCE role (allowing the user to create named types for custom
schemas) or even the DBA role, which allows the user to not only create
custom named types but alter and destroy them as well.
GRANT CONNECT, RESOURCE, DBA TO user_name;
Example
SQL> GRANT CONNECT, RESOURCE, DBA TO atnylaUser;
Grant succeeded.
SQL>
Assigning Privileges
Next you’ll want to ensure the user has privileges to actually connect to the
database and create a session using GRANT CREATE SESSION . We’ll also
combine that with all privileges using GRANT ANY PRIVILEGES .
GRANT CREATE SESSION GRANT ANY PRIVILEGE TO user_name;
We also need to ensure our new user has disk space allocated in the system to
actually create or modify tables and data, so we’ll GRANT TABLESPACE like so:
GRANT UNLIMITED TABLESPACE TO user_name;
Example
SQL> GRANT UNLIMITED TABLESPACE TO atnylaUser;
Grant succeeded.
SQL>
Table Privileges
While not typically necessary in newer versions of Oracle, some older
installations may require that you manually specify the access rights the new
user has to a specific schema and database tables.
For example, if we want our user_name user to have the ability to
perform SELECT , UPDATE , INSERT , and DELETE capabilities on
the books table, we might execute the following GRANT statement:
GRANT
SELECT,
INSERT,
UPDATE,
DELETE
ON
schema.books
TO
user_name;
This ensures that atnylaUser can perform the four basic statements for
the books table that is part of the schema schema.
©Privacy
Contact information: atnyla (http://www.atnyla.com).
By: Rumman Ansari (http://www.atnyla.com/tuition).
Log into new User in SQL
In this section we are going to teach you how to connect to your newly
created user. In my case the user name is atnylaUser and the password
rumman_ansari. If you don't know how to create a new user see previous
tutorials.
Below is our query to connect with our newly created user.
SQL Query
SQL> connect atnylaUser/rumman_ansari;
Connected.
SQL>
Now I am connected to my user. If you want to cross check whether you are
connected or not run the below query.
SQL Query
SQL> SHOW user;
USER is "ATNYLAUSER"
SQL>
So here it is, we are connected with our user: atnylaUser
Hey, Now we are ready to create your own database inside our own user. So I
think you areaready to create a new database. To create a new database check
the next section.
©Privacy
Contact information: atnyla (http://www.atnyla.com).
By: Rumman Ansari (http://www.atnyla.com/tuition).
 View 6
Create Database in SQL
How to create a Database
The SQL CREATE DATABASE statement is used to create a new SQL
database.
SQL> connect system/alone#i#4;
Connected.
SQL> GRANT ALL PRIVILEGES TO atnylaUser;
Grant succeeded.
SQL>
Syntax
The following SQL statement is the syntax for Creating a database.
CREATE DATABASE databasename;
CREATE DATABASE Example
This is an example SQL statement for the above syntax.
SQL> CREATE DATABASE testDataBase;
Make sure you have the admin privilege before creating any database. Once a
database is created, you can check it in the list of databases as follows ?
SQL> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| AMROOD |
| TUTORIALSPOINT |
| mysql |
| orig |
| test |
| testDataBase |
+--------------------+
7 rows in set (0.00 sec)
©Privacy
 View 2
Contact information: atnyla (http://www.atnyla.com).
By: Rumman Ansari (http://www.atnyla.com/tuition).
Delete or Drop Database in SQL
The SQL DROP DATABASE statement is used to drop an existing database
in SQL schema.
Syntax
The basic syntax of DROP DATABASE statement is as follows −
DROP DATABASE DatabaseName;
Always the database name should be unique within the RDBMS.
Example
If you want to delete an existing database <testDataBase>, then the DROP
DATABASE statement would be as shown below −
SQL> DROP DATABASE testDataBase;
NOTE − Be careful before using this operation because by deleting an
existing database would result in loss of complete information stored in the
database.
Make sure you have the admin privilege before dropping any database. Once
a database is dropped, you can check it in the list of the databases as shown
below −
SQL> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| AMROOD |
| TUTORIALSPOINT |
| mysql |
| orig |
| test |
+--------------------+
6 rows in set (0.00 sec)
©Privacy
Contact information: atnyla (http://www.atnyla.com).
By: Rumman Ansari (http://www.atnyla.com/tuition).
 View 1
Use a particular Database in SQL
When you have multiple databases in your SQL Schema, then before starting
your operation, you would need to select a database where all the operations
would be performed.
The SQL USE statement is used to select any existing database in the SQL
schema.
Syntax
The basic syntax of the USE statement is as shown below −
USE DatabaseName;
Always the database name should be unique within the RDBMS.
Example
You can check the available databases as shown below −
SQL> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| AMROOD |
| TUTORIALSPOINT |
| mysql |
| orig |
| test |
+--------------------+
6 rows in set (0.00 sec)
Now, if you want to work with the AMROOD database, then you can execute
the following SQL command and start working with the AMROOD database.
SQL> USE AMROOD;
©Privacy
Contact information: atnyla (http://www.atnyla.com).
By: Rumman Ansari (http://www.atnyla.com/tuition).
 View 5
Create table in SQL
The SQL CREATE TABLE Statement
The CREATE TABLE statement is used to create a new table in a database.
Syntax
The following SQL statement is syntax for CREATE TABLE.
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
.....
columnN datatype
);
CREATE TABLE is the keyword telling the database system what you want to
do. In this case, you want to create a new table. The unique name or
identifier for the table follows the CREATE TABLE statement.
Then in brackets comes the list defining each column in the table and what
sort of data type it is. The syntax becomes clearer with the following example.
A copy of an existing table can be created using a combination of the CREATE
TABLE statement and the SELECT statement.
The column parameters specify the names of the columns of the table.
The datatype parameter specifies the type of data the column can hold (e.g.
varchar, integer, date, etc.).
Example
This a example SQL statement for the above syntax.
The following code block is an example, which creates a Human table with an
ID as NOT NULL are the constraints showing that these fields cannot be
NULL while creating records in this table ?
 View 3
CREATE TABLE Human(
ID INT NOT NULL,
FIRST_NAME VARCHAR (20) NOT NULL,
LAST_NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2)
);
After placing the above code it will show you Table created. statement.
Example From SQL PLUS screen
SQL> CREATE TABLE Human(
2 ID INT NOT NULL,
3 FIRST_NAME VARCHAR (20) NOT NULL,
4 LAST_NAME VARCHAR (20) NOT NULL,
5 AGE INT NOT NULL,
6 ADDRESS CHAR (25) ,
7 SALARY DECIMAL (18, 2)
8 );
Table created.
SQL>
You can verify if your table has been created successfully by looking at the
message displayed by the SQL server, otherwise you can use the DESC
command as follows ?
Remember: table name should be always unique. Means no two table
have same name. table name should be always different
Another Example with primary key
SQL> DESC Human;
Name Null? Type
----------------------------------------- -------- --------------------------
ID NOT NULL NUMBER(38)
FIRST_NAME NOT NULL VARCHAR2(20)
LAST_NAME NOT NULL VARCHAR2(20)
AGE NOT NULL NUMBER(38)
ADDRESS CHAR(25)
SALARY NUMBER(18,2)
SQL>
Syntax
The following SQL statement is syntax for CREATE TABLE with primary key.
CREATE TABLE Human(
ID INT NOT NULL,
FIRST_NAME VARCHAR (20) NOT NULL,
LAST_NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
Example
This a example SQL statement for the above syntax.
SQL> CREATE TABLE Human(
2 ID INT NOT NULL,
3 FIRST_NAME VARCHAR (20) NOT NULL,
4 LAST_NAME VARCHAR (20) NOT NULL,
5 AGE INT NOT NULL,
6 ADDRESS CHAR (25) ,
7 SALARY DECIMAL (18, 2),
8 PRIMARY KEY (ID)
9 );
Table created.
SQL>
You can verify if your table has been created successfully by looking at the
message displayed by the SQL server, otherwise you can use the DESC
command as follows ?
SQL> DESC Human;
Some Another Examples
Example 1: CREATE TABLE Persons
SQL Syntax
CREATE TABLE Persons (
PersonID int,
FirstName varchar(255),
LastName varchar(255),
Address varchar(255),
City varchar(255)
);
Example 2: CREATE TABLE Client_master_21
SQL Syntax
CREATE TABLE Client_master_21(client_no varchar2(6) not null,
name varchar2(20) not null,
address1 varchar2(30),
address2 varchar2(30),
city varchar2(15) not null,
pincode number(6) not null,
state varchar2(15) not null,
bal_due number(10,2) not null
);
Example 3: CREATE TABLE Product_master_21
PERSONID NUMBER(38)
FIRSTNAME VARCHAR2(255)
LASTNAME VARCHAR2(255)
ADDRESS VARCHAR2(255)
CITY VARCHAR2(255)
CLIENT_NO NOT NULL VARCHAR2(6)
NAME NOT NULL VARCHAR2(20)
ADDRESS1 VARCHAR2(30)
ADDRESS2 VARCHAR2(30)
CITY NOT NULL VARCHAR2(15)
PINCODE NOT NULL NUMBER(6)
STATE NOT NULL VARCHAR2(15)
BAL_DUE NOT NULL NUMBER(10,2)
SQL Syntax
CREATE TABLE Product_master_21( product_no varchar2(6) not null,
description varchar2(15) not null,
profit_percent number(4,2) not null,
unit_measure varchar2(10) not null,
qty_on_hand number(8) not null,
recode_lvl number(8) not null,
sell_price number(8,2) not null,
cost_price number(8,2) not null
);
Example 4: CREATE TABLE Salesman_master_21
SQL Syntax
PRODUCT_NO NOT NULL VARCHAR2(6)
DESCRIPTION NOT NULL VARCHAR2(15)
PROFIT_PERCENT NOT NULL NUMBER(4,2)
UNIT_MEASURE NOT NULL VARCHAR2(10)
QTY_ON_HAND NOT NULL NUMBER(8)
RECODE_LVL NOT NULL NUMBER(8)
SELL_PRICE NOT NULL NUMBER(8,2)
COST_PRICE NOT NULL NUMBER(8,2)
SALESMAN_NO VARCHAR2(6)
SALESMAN_NAME NOT NULL VARCHAR2(20)
ADDRESS1 NOT NULL VARCHAR2(30)
ADDRESS2 NOT NULL VARCHAR2(30)
CITY NOT NULL VARCHAR2(20)
PINCODE NOT NULL NUMBER(6)
STATE NOT NULL VARCHAR2(15)
SAL_AMT NOT NULL NUMBER(8,2)
TGT_TO_GET NOT NULL NUMBER(6,2)
YTD_SALES NOT NULL NUMBER(6,2)
REMARKS NOT NULL VARCHAR2(10)
CREATE TABLE Salesman_master_21( salesman_no varchar2(6),
salesman_name varchar2(20) not null,
address1 varchar2(30) not null,
address2 varchar2(30) not null,
city varchar2(20) not null,
pincode number(6) not null,
state varchar2(15) not null,
sal_amt number(8,2) not null,
tgt_to_get number(6,2) not null,
ytd_sales number(6,2) not null,
remarks varchar2(10) not null
);
©Privacy
Contact information: atnyla (http://www.atnyla.com).
By: Rumman Ansari (http://www.atnyla.com/tuition).
Drop table in SQL
The SQL DROP TABLE statement is used to remove a table definition and all
the data, indexes, triggers, constraints and permission specifications for that
table.
Note: Be careful before dropping a table. Deleting a table will result in
loss of complete information stored in the table!
Syntax
The following SQL statement is the syntax for DROP TABLE
DROP TABLE table_name;
Example
A Human table already present in my database. Let us first verify the Human
table and then we will delete it from the database as shown below ?
This means that the Human table is available in the database, so let us now
drop it as shown below.
Now Drop the table using DROP TABLE table_name query.
SQL> DROP TABLE Human;
Table dropped.
Here it is, our Human table is deleted. Now Let us verify again the Human
table.
 View 3
SQL> DESC Human;
Name Null? Type
----------------------------------------- -------- ----------------------------
ID NOT NULL NUMBER(38)
FIRST_NAME NOT NULL VARCHAR2(20)
LAST_NAME NOT NULL VARCHAR2(20)
AGE NOT NULL NUMBER(38)
ADDRESS CHAR(25)
SALARY NUMBER(18,2)
This means that the Human table is not available in the database. for that
it's showing us object Human does not exist.
Another Examples
Below tables are already created in my database. Now I want to delete below
tables. Let's start.
Drop table Persons
SQL> DROP TABLE Persons;
Table dropped.
Drop table Client_master_21
SQL> DROP TABLE Client_master_21;
Table dropped.
Drop table Product_master_21
SQL> DROP TABLE Product_master_21;
Table dropped.
Drop table
SQL> DROP TABLE Salesman_master_21;
Table dropped.
©Privacy
Contact information: atnyla (http://www.atnyla.com).
By: Rumman Ansari (http://www.atnyla.com/tuition).
SQL> DESC Human;
ERROR:
ORA-04043: object Human does not exist
Insert Data into the table
The SQL INSERT INTO Statement is used to add new rows of data to a table
in the database.
Syntax
There are two basic syntaxes of the INSERT INTO statement which are
shown below.
The following SQL statement is syntax for INSERT INTO database
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
Here, column1, column2, column3,...columnN are the names of the columns
in the table into which you want to insert the data.
You may not need to specify the column(s) name in the SQL query if you are
adding values for all the columns of the table. But make sure the order of the
values is in the same order as the columns in the table.
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
Example
INSERT INTO Human (ID,FIRST_NAME,LAST_NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Rumman','Ansari', 32, 'Ahmedabad', 2000.00 );
INSERT INTO Human (ID,FIRST_NAME,LAST_NAME,AGE,ADDRESS,SALARY)
VALUES (2, 'Musar','Mondal', 25, 'Delhi', 1500.00 );
INSERT INTO Human (ID,FIRST_NAME,LAST_NAME,AGE,ADDRESS,SALARY)
VALUES (3, 'Osman','Sk', 23, 'Kota', 2000.00 );
INSERT INTO Human (ID,FIRST_NAME,LAST_NAME,AGE,ADDRESS,SALARY)
VALUES (4, 'Badsha','Roy', 25, 'Mumbai', 6500.00 );
INSERT INTO Human (ID,FIRST_NAME,LAST_NAME,AGE,ADDRESS,SALARY)
VALUES (5, 'Alamgir','Roy', 27, 'Bhopal', 8500.00 );
INSERT INTO Human (ID,FIRST_NAME,LAST_NAME,AGE,ADDRESS,SALARY)
VALUES (6, 'Rajesh','Roy', 22, 'MP', 4500.00 );
 View 5
You can create a record in the Human table by using the second syntax as
shown below.
INSERT INTO Human
VALUES (7, 'Rambo','Azmi', 24, 'Indore', 10000.00 );
After installation it will show like the below
SQL> INSERT INTO Human (ID,FIRST_NAME,LAST_NAME,AGE,ADDRESS,SALARY)
2 VALUES (2, 'Musar','Mondal', 25, 'Delhi', 1500.00 );
1 row created.
All the above statements would produce the following records in the Human
table as shown below.
Insert Data Only in Specified Columns
It is also possible to only insert data in specific columns.
The following SQL statement will insert a new record, but only insert data in
the "FIRSTNAME", "LASTNAME", and "ADDRESS" columns:
But Remember other columns should not be NOT NULL columns
INSERT INTO Human (FIRST_NAME,LAST_NAME,ADDRESS)
VALUES ('ib','MAM','Delhi');
Populate one table using another table
You can populate the data into a table through the select statement over
another table; provided the other table has a set of fields, which are required
to populate the first table.
Here is the syntax :
INSERT INTO First_Table_Name [(column1, column2, ... columnN)]
SELECT column1, column2, ...columnN
FROM Second_Table_Name
[WHERE condition];
---------- -------------------- -------------------- ---------- ----------------
ID FIRST_NAME LAST_NAME AGE ADDRESS
---------- -------------------- -------------------- ---------- ----------------
1 Rumman Ansari 32 Ahmedabad
2 Musar Mondal 25 Delhi
3 Osman Sk 23 Kota
4 Badsha Roy 25 Mumbai
5 Alamgir Roy 27 Bhopal
6 Rajesh Roy 22 MP
7 Rambo Azmi 24 Indore
---------- -------------------- -------------------- ---------- ----------------
Insert Data into Client_master_21 table
Sql Query
Insert Data into Product_master_21 table
SQL Code
Insert Data into Salesman_master_21 table
------ | ---------- --------------- | ---------- | --------------- | ---------
CLIENT | NAME | PINCODE | STATE | BAL_DUE
------ | ---------- --------------- | ---------- | --------------- | ---------
C00001 | Amit Saman | Kolkata | 700001 | West | 15000.
| ta | | | Bengal |
C00002 | Tapos Das | Mumbai | 400012 | Maharashtra |
C00003 | Anup Maiti | Mumbai | 400014 | Maharashtra | 500
C00004 | Bimal Roy | Chennai | 600018 | Tamil Nadu |
C00005 | Moni Kar | Kolkata | 700017 | West Bengal | 200
C00006 | AR Khan | Delhi | 700024 | Delhi |
------ | ---------- --------------- | ---------- | --------------- | ---------
INSERT INTO Client_master_21 VALUES('C00001','Amit Samanta','','','Kolkata',70
INSERT INTO Client_master_21 VALUES('C00002','Tapos Das','','','Mumbai',400012
INSERT INTO Client_master_21 VALUES('C00003','Anup Maiti','','','Mumbai',40001
INSERT INTO Client_master_21 VALUES('C00004','Bimal Roy','','','Chennai',60001
INSERT INTO Client_master_21 VALUES('C00005','Moni Kar','','','Kolkata',700017
INSERT INTO Client_master_21 VALUES('C00006','AR Khan','','','Delhi',700024,'D
------ | --------------- | -------------- | ---------- | ----------- | ---------
PRODUC | DESCRIPTION | PROFIT_PERCENT | UNIT_MEASU | QTY_ON_HAND | RECODE_
------ | --------------- | -------------- | ---------- | ----------- | ---------
P00001 | 1.44 | 5 | piece | 100 | 2
| floppies | | | |
P03453 | 6 | 5 | piece | 10 | 2
P06734 | 5 | 5 | piece | 20 | 2
P07868 | 5 | 5 | piece | 10 | 2
P07885 | 25 | 5 | piece | 10 | 2
------ | --------------- | -------------- | ---------- | ----------- | ---------
INSERT INTO Product_master_21 VALUES('P00001','1.44 floppies',5,'piece',100,2
INSERT INTO Product_master_21 VALUES('P03453','6',5,'piece',10,20,3,11280);
INSERT INTO Product_master_21 VALUES('P06734','5',5,'piece',20,20,5,100);
INSERT INTO Product_master_21 VALUES('P07868','5',5,'piece',10,20,3,1000);
INSERT INTO Product_master_21 VALUES('P07885','25',5,'piece',10,20,3,5100);
©Privacy
Contact information: atnyla (http://www.atnyla.com).
By: Rumman Ansari (http://www.atnyla.com/tuition).
INSERT INTO Salesman_master_21
VALUES('S00001','Krim','A/4','Alipore','CityA',700012,'StateE',3000,100,50,'Go
INSERT INTO Salesman_master_21
VALUES('S00002','Azad','65','Barabazar','CityB',700001,'StateF',3000,200,100,'
INSERT INTO Salesman_master_21 VALUES('S00003','Samir','p-
7','Chadni','CityC',700022,'StateG',3000,200,100,'Good');
INSERT INTO Salesman_master_21
VALUES('S00004','Anindya','A/7','Saltlake','CityD',700091,'StateH',3500,500,15

Más contenido relacionado

La actualidad más candente

Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)Ishucs
 
Structured Query Language (SQL)
Structured Query Language (SQL)Structured Query Language (SQL)
Structured Query Language (SQL)Syed Hassan Ali
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries InformationNishant Munjal
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functionsVikas Gupta
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introductionSmriti Jain
 
Introduction to Oracle Database
Introduction to Oracle DatabaseIntroduction to Oracle Database
Introduction to Oracle Databasepuja_dhar
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL CommandsShrija Madhu
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functionsAmrit Kaur
 
What is SQL Server?
What is SQL Server?What is SQL Server?
What is SQL Server?CPD INDIA
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentationNITISH KUMAR
 

La actualidad más candente (20)

SQL Basics
SQL BasicsSQL Basics
SQL Basics
 
SQL Commands
SQL Commands SQL Commands
SQL Commands
 
Chapter 4 Structured Query Language
Chapter 4 Structured Query LanguageChapter 4 Structured Query Language
Chapter 4 Structured Query Language
 
Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
Structured Query Language (SQL)
Structured Query Language (SQL)Structured Query Language (SQL)
Structured Query Language (SQL)
 
SQL Queries Information
SQL Queries InformationSQL Queries Information
SQL Queries Information
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
SQL select statement and functions
SQL select statement and functionsSQL select statement and functions
SQL select statement and functions
 
Basic SQL and History
 Basic SQL and History Basic SQL and History
Basic SQL and History
 
Sql operator
Sql operatorSql operator
Sql operator
 
SQL commands
SQL commandsSQL commands
SQL commands
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
Introduction to Oracle Database
Introduction to Oracle DatabaseIntroduction to Oracle Database
Introduction to Oracle Database
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functions
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
 
What is SQL Server?
What is SQL Server?What is SQL Server?
What is SQL Server?
 
Sql queries presentation
Sql queries presentationSql queries presentation
Sql queries presentation
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 

Similar a Sql tutorial (20)

chapter-14-sql-commands.pdf
chapter-14-sql-commands.pdfchapter-14-sql-commands.pdf
chapter-14-sql-commands.pdf
 
SQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics CoveredSQL Complete Tutorial. All Topics Covered
SQL Complete Tutorial. All Topics Covered
 
lovely
lovelylovely
lovely
 
ORACLE PL SQL
ORACLE PL SQLORACLE PL SQL
ORACLE PL SQL
 
sql-commands.pdf
sql-commands.pdfsql-commands.pdf
sql-commands.pdf
 
Sql commands
Sql commandsSql commands
Sql commands
 
Sql commands
Sql commandsSql commands
Sql commands
 
Database Management Lab -SQL Queries
Database Management Lab -SQL Queries Database Management Lab -SQL Queries
Database Management Lab -SQL Queries
 
Cursors, triggers, procedures
Cursors, triggers, proceduresCursors, triggers, procedures
Cursors, triggers, procedures
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
Structured Query Language (SQL).pptx
Structured Query Language (SQL).pptxStructured Query Language (SQL).pptx
Structured Query Language (SQL).pptx
 
Oracle Introduction
Oracle Introduction Oracle Introduction
Oracle Introduction
 
Dbmsunit v
Dbmsunit vDbmsunit v
Dbmsunit v
 
Oracle notes
Oracle notesOracle notes
Oracle notes
 
Sql intro & ddl 1
Sql intro & ddl 1Sql intro & ddl 1
Sql intro & ddl 1
 
Sql intro & ddl 1
Sql intro & ddl 1Sql intro & ddl 1
Sql intro & ddl 1
 
MySQL intro
MySQL introMySQL intro
MySQL intro
 
MySQL intro
MySQL introMySQL intro
MySQL intro
 
Relational Database Language.pptx
Relational Database Language.pptxRelational Database Language.pptx
Relational Database Language.pptx
 
SQL for interview
SQL for interviewSQL for interview
SQL for interview
 

Más de Rumman Ansari

C programming exercises and solutions
C programming exercises and solutions C programming exercises and solutions
C programming exercises and solutions Rumman Ansari
 
Java Tutorial best website
Java Tutorial best websiteJava Tutorial best website
Java Tutorial best websiteRumman Ansari
 
Java Questions and Answers
Java Questions and AnswersJava Questions and Answers
Java Questions and AnswersRumman Ansari
 
C program to write c program without using main function
C program to write c program without using main functionC program to write c program without using main function
C program to write c program without using main functionRumman Ansari
 
Steps for c program execution
Steps for c program executionSteps for c program execution
Steps for c program executionRumman Ansari
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c programRumman Ansari
 
My first program in c, hello world !
My first program in c, hello world !My first program in c, hello world !
My first program in c, hello world !Rumman Ansari
 
How c program execute in c program
How c program execute in c program How c program execute in c program
How c program execute in c program Rumman Ansari
 
What is token c programming
What is token c programmingWhat is token c programming
What is token c programmingRumman Ansari
 
What is identifier c programming
What is identifier c programmingWhat is identifier c programming
What is identifier c programmingRumman Ansari
 
What is keyword in c programming
What is keyword in c programmingWhat is keyword in c programming
What is keyword in c programmingRumman Ansari
 
Type casting in c programming
Type casting in c programmingType casting in c programming
Type casting in c programmingRumman Ansari
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11Rumman Ansari
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9Rumman Ansari
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8Rumman Ansari
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7Rumman Ansari
 
C Programming Language Part 6
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6Rumman Ansari
 
C Programming Language Part 5
C Programming Language Part 5C Programming Language Part 5
C Programming Language Part 5Rumman Ansari
 
C Programming Language Part 4
C Programming Language Part 4C Programming Language Part 4
C Programming Language Part 4Rumman Ansari
 

Más de Rumman Ansari (20)

C programming exercises and solutions
C programming exercises and solutions C programming exercises and solutions
C programming exercises and solutions
 
Java Tutorial best website
Java Tutorial best websiteJava Tutorial best website
Java Tutorial best website
 
Java Questions and Answers
Java Questions and AnswersJava Questions and Answers
Java Questions and Answers
 
servlet programming
servlet programmingservlet programming
servlet programming
 
C program to write c program without using main function
C program to write c program without using main functionC program to write c program without using main function
C program to write c program without using main function
 
Steps for c program execution
Steps for c program executionSteps for c program execution
Steps for c program execution
 
Pointer in c program
Pointer in c programPointer in c program
Pointer in c program
 
My first program in c, hello world !
My first program in c, hello world !My first program in c, hello world !
My first program in c, hello world !
 
How c program execute in c program
How c program execute in c program How c program execute in c program
How c program execute in c program
 
What is token c programming
What is token c programmingWhat is token c programming
What is token c programming
 
What is identifier c programming
What is identifier c programmingWhat is identifier c programming
What is identifier c programming
 
What is keyword in c programming
What is keyword in c programmingWhat is keyword in c programming
What is keyword in c programming
 
Type casting in c programming
Type casting in c programmingType casting in c programming
Type casting in c programming
 
C Programming Language Part 11
C Programming Language Part 11C Programming Language Part 11
C Programming Language Part 11
 
C Programming Language Part 9
C Programming Language Part 9C Programming Language Part 9
C Programming Language Part 9
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
C Programming Language Part 6
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6
 
C Programming Language Part 5
C Programming Language Part 5C Programming Language Part 5
C Programming Language Part 5
 
C Programming Language Part 4
C Programming Language Part 4C Programming Language Part 4
C Programming Language Part 4
 

Último

VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 

Último (20)

VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 

Sql tutorial

  • 2. About SQL Tutorial SQL stands for Structured Query Language. The SQL language as a query language, it can do much more than just query a database. It can define the structure of the data, modify data in the database, and specify security constraints. A database system provides a data definition language to specify the database schema and a data manipulation language to express database queries and updates. In practice, the data definition and data manipulation languages are not two separate languages; instead they simply form parts of a single database language, such as the widely used SQL language. About This Tutorial This SQL tutorial has been prepared for the beginners to help them understand the basic to advanced concepts of SQL. This tutorial will give you enough understanding on the various components of SQL along with suitable examples. Prerequisites Before you start practicing with various types of examples given in this tutorial, we are assuming that you are already aware about what a database is, especially the RDBMS and what is a computer programming language. Audience Students who are pursuing BCA, MCA, B.E/B.Tech (CS/IT/EC/EE/ME/Civil/Any other discipline) Students who have "Java Programming with SQL" in their curriculum Candidates who wish to switch from service based companies to product based Candidates who want to continue their career as JAVA developer with SQL Candidates who are passionate about coding ©Privacy Contact information: atnyla (http://www.atnyla.com). By: Rumman Ansari (http://www.atnyla.com/tuition).  View 11
  • 3. SQL Basic Overview What is SQL SQL stands for Structured Query Language and can be pronounced as SQL or sequel – (Structured Query Language) . Defined, SQL is a query language used for accessing and modifying information in one or more data tables and rows of a database. SQL DataBase In a simple manner, SQL is a non-procedural, English-like language that processes data in groups of records rather than one record at a time. Few SQL functions of are: Store data Modify data Retrieve data Delete data Create tables and other database objects A Brief History of SQL SQL Database Design IBM first developed SQL in 1970s. Also it is an ANSI/ISO standard. It has become a Standard Universal Language used by most of the relational database management systems (RDBMS). Some of the RDBMS systems are: Oracle, Microsoft SQL server, Sybase etc.  View 9 1970 − Dr. Edgar F. "Ted" Codd of IBM is known as the father of relational databases. He described a relational model for databases. 1974 − Structured Query Language appeared. 1978 − IBM worked to develop Codd's ideas and released a product named System/R. 1986 − IBM developed the first prototype of relational database and standardized by ANSI. The first relational database was released by Relational Software which later came to be known as Oracle.
  • 4. Most of these have provided their own implementation extensions, thus enhancing their RDBMS system features and making it a powerful tool. These RDBMS systems, all use the popular SQL commands SELECT, UPDATE, DELETE, INSERT, WHERE in similar format. SQL Database Table SQL database is constructed of a number of tables. In a business, SQL tables would be used to divide and simplify the different areas of the operation: Table for Customers, one for Vendors, Employees and so on. SQL Database Table Columns Each SQL table is made up of a number of columns, referred to as fields and run along the top of the table. Sql columns or fields have their content (object/data/info) defined into character types; such as text, date, numeric, integer, length to name a few. SQL Database Table Rows Each SQL table row, referred to a record, is located in the left column of the table. Sql record row will contain a string of data containing data matching up to each column field across the top. So, in a Customer table each customer record would consist of one row with data for the customer ID number, customer name, address, phone ... email and so on. Simple Overview of various SQL query SQL Query Types SELECT Statement Retrieve records from a table SELECT LIMIT Statement Retrieve records from a table and limit results SELECT TOP Statement Retrieve records from a table and limit results INSERT Statement Insert records into a table UPDATE Statement Update records in a table DELETE Statement Delete records from a table TRUNCATE TABLE Statement Delete all records from a table (no rollback) UNION Operator Combine 2 result sets (removes duplicates) UNION ALL Operator Combine 2 result sets (includes duplicates)
  • 5. INTERSECT Operator Intersection of 2 result sets MINUS Operator Result set of one minus the result set of another EXCEPT Operator Result set of one minus the result set of another SQL Comparison Operators Comparison Operators Operators such as =, <>, !=, >, <, and so on SQL Joins JOIN Tables Inner and Outer joins SQL Aliases ALIASES Create a temporary name for a column or table SQL Clauses DISTINCT Clause Retrieve unique records FROM Clause List tables and join information WHERE Clause Filter results ORDER BY Clause Sort query results GROUP BY Clause Group by one or more columns HAVING Clause Restrict the groups of returned rows SQL Functions COUNT Function Return the count of an expression SUM Function Return the sum of an expression MIN Function Return the min of an expression MAX Function Return the max of an expression AVG Function Return the average of an expression
  • 6. SQL Conditions AND Condition 2 or more conditions to be met OR Condition Any one of the conditions are met AND & OR Combining AND and OR conditions LIKE Condition Use wildcards in a WHERE clause IN Condition Alternative to multiple OR conditions NOT Condition Negate a condition IS NULL Condition Test for NULL value IS NOT NULL Condition Test for NOT NULL value BETWEEN Condition Retrieve within a range (inclusive) EXISTS Condition Condition is met if subquery returns at least one row SQL Tables and Views CREATE TABLE Create a table CREATE TABLE AS Create a table from another table's definition and data ALTER TABLE Add, modify or delete columns in a table; rename a table DROP TABLE Delete a table GLOBAL TEMP Tables Tables that are distinct within SQL session LOCAL TEMP Tables Tables that are distinct within modules and embedded SQL program SQL VIEW Virtual tables (views of other tables) ©Privacy Contact information: atnyla (http://www.atnyla.com). By: Rumman Ansari (http://www.atnyla.com/tuition).
  • 7. Query Language DDL, DML, DCL Data-definition language (DDL): The SQL DDL provides commands for defining relation schemas, deleting relations, and modifying relation schemas. DDL - Data Definition Language Command & Description CREATE Creates a new table, a view of a table, or other objother objectsatabase. ALTER Modifies an existing database object, such as a table. DROP Deletes an entire table, a view of a table or other objects in the database. Interactive data-manipulation language (DML): The SQL DML includes a query language based on both the relational algebra and the tuple relational calculus. It includes also commands to insert tuples into, delete tuples from, and modify tuples in the database. DML - Data Manipulation Language Command & Description SELECT Retrieves certain records from one or more tables. INSERT Creates a record. UPDATE Modifies records.  View 9
  • 8. DELETE Deletes records. View definition: The SQL DDL includes commands for defining views. Transaction control. SQL includes commands for specifying the beginning and ending of transactions. Embedded SQL and dynamic SQL. Embedded and dynamic SQL define how SQL statements can be embedded within general-purpose programming languages, such as C, C++, Java, PL/I, Cobol, Pascal, and Fortran. Integrity. The SQL DDL includes commands for specifying integrity constraints that the data stored in the database must satisfy. Updates that violate integrity constraints are disallowed. Authorization. The SQL DDL includes commands for specifying access rights to relations and views. DCL - Data Control Language Command & Description GRANT Gives a privilege to user. REVOKE Takes back privileges granted from user. ©Privacy Contact information: atnyla (http://www.atnyla.com). By: Rumman Ansari (http://www.atnyla.com/tuition).
  • 9. SQL Plus SQL*Plus is the most basic Oracle Database utility, with a basic command- line interface, commonly used by users, administrators, and programmers. Command types SQL*Plus understands five categories of text: 1. SQL statements 2. PL/SQL blocks 3. SQL*Plus internal commands, for example: environment control commands such as SET environment monitoring commands such as SHOW 4. Comments 5. External commands prefixed by the ! char Scripts can include all of these components. An Oracle programmer in the appropriately configured software environment can launch SQL*Plus, for example, by entering: $ sqlplus scott/tiger where the Oracle user scott has the password tiger . SQL*Plus then presents a prompt with the default form of: SQL> Interactive use can then start by entering a SQL statement (terminated by a semicolon), a PL/SQL block, or another command. For example: SQL> select 'Hello world' as example from dual; EXAMPLE -------------------------------- Hello world ©Privacy Contact information: atnyla (http://www.atnyla.com). By: Rumman Ansari (http://www.atnyla.com/tuition).  View 6
  • 10. Connect with a system user in SQL Create a new user inside Oracle Database After installation of Oracle software. We have to create a user inside it. In this tutorial, we are going to show you how to create a new user. So let's start. To create a new user. Start SQL PLUS form your windows menu. It flows the below statements for SQL 11g release 2 version. For this, you have to login in the system user for the highest privilege. First I will tell you how to Connect to the Oracle Database from SQL*Plus. To connect to Oracle Database from SQL*Plus: (Using SQL PLUS) After typing the username and password it will look like this. By default, the username is system and password in that password which you gave at the time of installation, in my case the password is alone#i#4  View 6
  • 11. To connect to Oracle Database from SQL*Plus: (Using CMD) 1. If you are on a Windows system, display a Windows command prompt. 2. At the command prompt, type sqlplus and press the key Enter. SQL*Plus starts and prompts you for your username. 3. Type your username and press the key Enter. SQL*Plus prompts you for your password. 4. Type your password and press the key Enter. For security, your password is not visible on your screen. The system connects you to an Oracle Database instance. You are in the SQL*Plus environment. At the SQL> prompt, you can enter and run SQL*Plus commands, SQL statements, PL/SQL statements, and operating system commands. To exit SQL*Plus, type exit and press the key Enter. Exiting SQL*Plus ends the SQL*Plus session, but does not shut down the Oracle Database instance. SQL*Plus: Release 11.2.0.1.0 Production on Wed Jul 18 22:30:24 2018 Copyright (c) 1982, 2010, Oracle. All rights reserved. Enter user-name: system Enter password: alone#i#4 (For security, your password is not visible on your Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options SQL>
  • 12. How to see the recent database user SQL> SHOW user; USER is "SYSTEM" SQL> ©Privacy Contact information: atnyla (http://www.atnyla.com). By: Rumman Ansari (http://www.atnyla.com/tuition). Microsoft Windows [Version 10.0.10240] (c) 2015 Microsoft Corporation. All rights reserved. C:Usersuser1>SQLPLUS SQL*Plus: Release 11.2.0.1.0 Production on Wed Jul 18 22:24:33 2018 Copyright (c) 1982, 2010, Oracle. All rights reserved. Enter user-name: system Enter password: alone#i#4 (For security, your password is not visible on your Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options SQL>
  • 13. Create a Database User in SQL As always, begin by connecting to your server where Oracle is hosted, then connect to Oracle itself as the SYSTEM account. The SYSTEM account is one of a handful of predefined administrative accounts generated automatically when Oracle is installed. SYSTEM is capable of most administrative tasks, but the task we’re particularly interested in is account management. Creating a User Once connected as SYSTEM , simply issue the CREATE USER command to generate a new account. Run the below query to check whether you are in SYSTEM user or not If you don't know how to connect with SYSTEM user please see the previous tutorial SQL> SHOW user; USER is "SYSTEM" SQL> CREATE USER user_name IDENTIFIED BY MyPassword; Example Here I am creating one user. In my case user name is Username: atnylaUser and password is: rumman_ansari SQL> CREATE USER atnylaUser IDENTIFIED BY rumman_ansari; User created. SQL> Here we’re simply creating a atnylaUser account that is IDENTIFIED or authenticated by the specified rumman_ansari . The Grant Statement  View 7
  • 14. With our user_name new account created, we can now begin adding privileges to the account using the GRANT statement. GRANT is a very powerful statement with many possible options, but the core functionality is to manage the privileges of both users and roles throughout the database. Providing Roles Typically, you’ll first want to assign privileges to the user through attaching the account to various roles, starting with the CONNECT role: GRANT CONNECT TO user_name; Example SQL> GRANT CONNECT TO atnylaUser; Grant succeeded. SQL> In some cases to create a more powerful user, you may also consider adding the RESOURCE role (allowing the user to create named types for custom schemas) or even the DBA role, which allows the user to not only create custom named types but alter and destroy them as well. GRANT CONNECT, RESOURCE, DBA TO user_name; Example SQL> GRANT CONNECT, RESOURCE, DBA TO atnylaUser; Grant succeeded. SQL> Assigning Privileges Next you’ll want to ensure the user has privileges to actually connect to the database and create a session using GRANT CREATE SESSION . We’ll also combine that with all privileges using GRANT ANY PRIVILEGES . GRANT CREATE SESSION GRANT ANY PRIVILEGE TO user_name;
  • 15. We also need to ensure our new user has disk space allocated in the system to actually create or modify tables and data, so we’ll GRANT TABLESPACE like so: GRANT UNLIMITED TABLESPACE TO user_name; Example SQL> GRANT UNLIMITED TABLESPACE TO atnylaUser; Grant succeeded. SQL> Table Privileges While not typically necessary in newer versions of Oracle, some older installations may require that you manually specify the access rights the new user has to a specific schema and database tables. For example, if we want our user_name user to have the ability to perform SELECT , UPDATE , INSERT , and DELETE capabilities on the books table, we might execute the following GRANT statement: GRANT SELECT, INSERT, UPDATE, DELETE ON schema.books TO user_name; This ensures that atnylaUser can perform the four basic statements for the books table that is part of the schema schema. ©Privacy Contact information: atnyla (http://www.atnyla.com). By: Rumman Ansari (http://www.atnyla.com/tuition).
  • 16. Log into new User in SQL In this section we are going to teach you how to connect to your newly created user. In my case the user name is atnylaUser and the password rumman_ansari. If you don't know how to create a new user see previous tutorials. Below is our query to connect with our newly created user. SQL Query SQL> connect atnylaUser/rumman_ansari; Connected. SQL> Now I am connected to my user. If you want to cross check whether you are connected or not run the below query. SQL Query SQL> SHOW user; USER is "ATNYLAUSER" SQL> So here it is, we are connected with our user: atnylaUser Hey, Now we are ready to create your own database inside our own user. So I think you areaready to create a new database. To create a new database check the next section. ©Privacy Contact information: atnyla (http://www.atnyla.com). By: Rumman Ansari (http://www.atnyla.com/tuition).  View 6
  • 17. Create Database in SQL How to create a Database The SQL CREATE DATABASE statement is used to create a new SQL database. SQL> connect system/alone#i#4; Connected. SQL> GRANT ALL PRIVILEGES TO atnylaUser; Grant succeeded. SQL> Syntax The following SQL statement is the syntax for Creating a database. CREATE DATABASE databasename; CREATE DATABASE Example This is an example SQL statement for the above syntax. SQL> CREATE DATABASE testDataBase; Make sure you have the admin privilege before creating any database. Once a database is created, you can check it in the list of databases as follows ? SQL> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | AMROOD | | TUTORIALSPOINT | | mysql | | orig | | test | | testDataBase | +--------------------+ 7 rows in set (0.00 sec) ©Privacy  View 2
  • 18. Contact information: atnyla (http://www.atnyla.com). By: Rumman Ansari (http://www.atnyla.com/tuition).
  • 19. Delete or Drop Database in SQL The SQL DROP DATABASE statement is used to drop an existing database in SQL schema. Syntax The basic syntax of DROP DATABASE statement is as follows − DROP DATABASE DatabaseName; Always the database name should be unique within the RDBMS. Example If you want to delete an existing database <testDataBase>, then the DROP DATABASE statement would be as shown below − SQL> DROP DATABASE testDataBase; NOTE − Be careful before using this operation because by deleting an existing database would result in loss of complete information stored in the database. Make sure you have the admin privilege before dropping any database. Once a database is dropped, you can check it in the list of the databases as shown below − SQL> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | AMROOD | | TUTORIALSPOINT | | mysql | | orig | | test | +--------------------+ 6 rows in set (0.00 sec) ©Privacy Contact information: atnyla (http://www.atnyla.com). By: Rumman Ansari (http://www.atnyla.com/tuition).  View 1
  • 20. Use a particular Database in SQL When you have multiple databases in your SQL Schema, then before starting your operation, you would need to select a database where all the operations would be performed. The SQL USE statement is used to select any existing database in the SQL schema. Syntax The basic syntax of the USE statement is as shown below − USE DatabaseName; Always the database name should be unique within the RDBMS. Example You can check the available databases as shown below − SQL> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | AMROOD | | TUTORIALSPOINT | | mysql | | orig | | test | +--------------------+ 6 rows in set (0.00 sec) Now, if you want to work with the AMROOD database, then you can execute the following SQL command and start working with the AMROOD database. SQL> USE AMROOD; ©Privacy Contact information: atnyla (http://www.atnyla.com). By: Rumman Ansari (http://www.atnyla.com/tuition).  View 5
  • 21. Create table in SQL The SQL CREATE TABLE Statement The CREATE TABLE statement is used to create a new table in a database. Syntax The following SQL statement is syntax for CREATE TABLE. CREATE TABLE table_name( column1 datatype, column2 datatype, column3 datatype, ..... columnN datatype ); CREATE TABLE is the keyword telling the database system what you want to do. In this case, you want to create a new table. The unique name or identifier for the table follows the CREATE TABLE statement. Then in brackets comes the list defining each column in the table and what sort of data type it is. The syntax becomes clearer with the following example. A copy of an existing table can be created using a combination of the CREATE TABLE statement and the SELECT statement. The column parameters specify the names of the columns of the table. The datatype parameter specifies the type of data the column can hold (e.g. varchar, integer, date, etc.). Example This a example SQL statement for the above syntax. The following code block is an example, which creates a Human table with an ID as NOT NULL are the constraints showing that these fields cannot be NULL while creating records in this table ?  View 3
  • 22. CREATE TABLE Human( ID INT NOT NULL, FIRST_NAME VARCHAR (20) NOT NULL, LAST_NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2) ); After placing the above code it will show you Table created. statement. Example From SQL PLUS screen SQL> CREATE TABLE Human( 2 ID INT NOT NULL, 3 FIRST_NAME VARCHAR (20) NOT NULL, 4 LAST_NAME VARCHAR (20) NOT NULL, 5 AGE INT NOT NULL, 6 ADDRESS CHAR (25) , 7 SALARY DECIMAL (18, 2) 8 ); Table created. SQL> You can verify if your table has been created successfully by looking at the message displayed by the SQL server, otherwise you can use the DESC command as follows ? Remember: table name should be always unique. Means no two table have same name. table name should be always different Another Example with primary key SQL> DESC Human; Name Null? Type ----------------------------------------- -------- -------------------------- ID NOT NULL NUMBER(38) FIRST_NAME NOT NULL VARCHAR2(20) LAST_NAME NOT NULL VARCHAR2(20) AGE NOT NULL NUMBER(38) ADDRESS CHAR(25) SALARY NUMBER(18,2) SQL>
  • 23. Syntax The following SQL statement is syntax for CREATE TABLE with primary key. CREATE TABLE Human( ID INT NOT NULL, FIRST_NAME VARCHAR (20) NOT NULL, LAST_NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL, ADDRESS CHAR (25) , SALARY DECIMAL (18, 2), PRIMARY KEY (ID) ); Example This a example SQL statement for the above syntax. SQL> CREATE TABLE Human( 2 ID INT NOT NULL, 3 FIRST_NAME VARCHAR (20) NOT NULL, 4 LAST_NAME VARCHAR (20) NOT NULL, 5 AGE INT NOT NULL, 6 ADDRESS CHAR (25) , 7 SALARY DECIMAL (18, 2), 8 PRIMARY KEY (ID) 9 ); Table created. SQL> You can verify if your table has been created successfully by looking at the message displayed by the SQL server, otherwise you can use the DESC command as follows ? SQL> DESC Human; Some Another Examples Example 1: CREATE TABLE Persons
  • 24. SQL Syntax CREATE TABLE Persons ( PersonID int, FirstName varchar(255), LastName varchar(255), Address varchar(255), City varchar(255) ); Example 2: CREATE TABLE Client_master_21 SQL Syntax CREATE TABLE Client_master_21(client_no varchar2(6) not null, name varchar2(20) not null, address1 varchar2(30), address2 varchar2(30), city varchar2(15) not null, pincode number(6) not null, state varchar2(15) not null, bal_due number(10,2) not null ); Example 3: CREATE TABLE Product_master_21 PERSONID NUMBER(38) FIRSTNAME VARCHAR2(255) LASTNAME VARCHAR2(255) ADDRESS VARCHAR2(255) CITY VARCHAR2(255) CLIENT_NO NOT NULL VARCHAR2(6) NAME NOT NULL VARCHAR2(20) ADDRESS1 VARCHAR2(30) ADDRESS2 VARCHAR2(30) CITY NOT NULL VARCHAR2(15) PINCODE NOT NULL NUMBER(6) STATE NOT NULL VARCHAR2(15) BAL_DUE NOT NULL NUMBER(10,2)
  • 25. SQL Syntax CREATE TABLE Product_master_21( product_no varchar2(6) not null, description varchar2(15) not null, profit_percent number(4,2) not null, unit_measure varchar2(10) not null, qty_on_hand number(8) not null, recode_lvl number(8) not null, sell_price number(8,2) not null, cost_price number(8,2) not null ); Example 4: CREATE TABLE Salesman_master_21 SQL Syntax PRODUCT_NO NOT NULL VARCHAR2(6) DESCRIPTION NOT NULL VARCHAR2(15) PROFIT_PERCENT NOT NULL NUMBER(4,2) UNIT_MEASURE NOT NULL VARCHAR2(10) QTY_ON_HAND NOT NULL NUMBER(8) RECODE_LVL NOT NULL NUMBER(8) SELL_PRICE NOT NULL NUMBER(8,2) COST_PRICE NOT NULL NUMBER(8,2) SALESMAN_NO VARCHAR2(6) SALESMAN_NAME NOT NULL VARCHAR2(20) ADDRESS1 NOT NULL VARCHAR2(30) ADDRESS2 NOT NULL VARCHAR2(30) CITY NOT NULL VARCHAR2(20) PINCODE NOT NULL NUMBER(6) STATE NOT NULL VARCHAR2(15) SAL_AMT NOT NULL NUMBER(8,2) TGT_TO_GET NOT NULL NUMBER(6,2) YTD_SALES NOT NULL NUMBER(6,2) REMARKS NOT NULL VARCHAR2(10)
  • 26. CREATE TABLE Salesman_master_21( salesman_no varchar2(6), salesman_name varchar2(20) not null, address1 varchar2(30) not null, address2 varchar2(30) not null, city varchar2(20) not null, pincode number(6) not null, state varchar2(15) not null, sal_amt number(8,2) not null, tgt_to_get number(6,2) not null, ytd_sales number(6,2) not null, remarks varchar2(10) not null ); ©Privacy Contact information: atnyla (http://www.atnyla.com). By: Rumman Ansari (http://www.atnyla.com/tuition).
  • 27. Drop table in SQL The SQL DROP TABLE statement is used to remove a table definition and all the data, indexes, triggers, constraints and permission specifications for that table. Note: Be careful before dropping a table. Deleting a table will result in loss of complete information stored in the table! Syntax The following SQL statement is the syntax for DROP TABLE DROP TABLE table_name; Example A Human table already present in my database. Let us first verify the Human table and then we will delete it from the database as shown below ? This means that the Human table is available in the database, so let us now drop it as shown below. Now Drop the table using DROP TABLE table_name query. SQL> DROP TABLE Human; Table dropped. Here it is, our Human table is deleted. Now Let us verify again the Human table.  View 3 SQL> DESC Human; Name Null? Type ----------------------------------------- -------- ---------------------------- ID NOT NULL NUMBER(38) FIRST_NAME NOT NULL VARCHAR2(20) LAST_NAME NOT NULL VARCHAR2(20) AGE NOT NULL NUMBER(38) ADDRESS CHAR(25) SALARY NUMBER(18,2)
  • 28. This means that the Human table is not available in the database. for that it's showing us object Human does not exist. Another Examples Below tables are already created in my database. Now I want to delete below tables. Let's start. Drop table Persons SQL> DROP TABLE Persons; Table dropped. Drop table Client_master_21 SQL> DROP TABLE Client_master_21; Table dropped. Drop table Product_master_21 SQL> DROP TABLE Product_master_21; Table dropped. Drop table SQL> DROP TABLE Salesman_master_21; Table dropped. ©Privacy Contact information: atnyla (http://www.atnyla.com). By: Rumman Ansari (http://www.atnyla.com/tuition). SQL> DESC Human; ERROR: ORA-04043: object Human does not exist
  • 29. Insert Data into the table The SQL INSERT INTO Statement is used to add new rows of data to a table in the database. Syntax There are two basic syntaxes of the INSERT INTO statement which are shown below. The following SQL statement is syntax for INSERT INTO database INSERT INTO TABLE_NAME (column1, column2, column3,...columnN) VALUES (value1, value2, value3,...valueN); Here, column1, column2, column3,...columnN are the names of the columns in the table into which you want to insert the data. You may not need to specify the column(s) name in the SQL query if you are adding values for all the columns of the table. But make sure the order of the values is in the same order as the columns in the table. INSERT INTO table_name VALUES (value1, value2, value3, ...); Example INSERT INTO Human (ID,FIRST_NAME,LAST_NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Rumman','Ansari', 32, 'Ahmedabad', 2000.00 ); INSERT INTO Human (ID,FIRST_NAME,LAST_NAME,AGE,ADDRESS,SALARY) VALUES (2, 'Musar','Mondal', 25, 'Delhi', 1500.00 ); INSERT INTO Human (ID,FIRST_NAME,LAST_NAME,AGE,ADDRESS,SALARY) VALUES (3, 'Osman','Sk', 23, 'Kota', 2000.00 ); INSERT INTO Human (ID,FIRST_NAME,LAST_NAME,AGE,ADDRESS,SALARY) VALUES (4, 'Badsha','Roy', 25, 'Mumbai', 6500.00 ); INSERT INTO Human (ID,FIRST_NAME,LAST_NAME,AGE,ADDRESS,SALARY) VALUES (5, 'Alamgir','Roy', 27, 'Bhopal', 8500.00 ); INSERT INTO Human (ID,FIRST_NAME,LAST_NAME,AGE,ADDRESS,SALARY) VALUES (6, 'Rajesh','Roy', 22, 'MP', 4500.00 );  View 5
  • 30. You can create a record in the Human table by using the second syntax as shown below. INSERT INTO Human VALUES (7, 'Rambo','Azmi', 24, 'Indore', 10000.00 ); After installation it will show like the below SQL> INSERT INTO Human (ID,FIRST_NAME,LAST_NAME,AGE,ADDRESS,SALARY) 2 VALUES (2, 'Musar','Mondal', 25, 'Delhi', 1500.00 ); 1 row created. All the above statements would produce the following records in the Human table as shown below. Insert Data Only in Specified Columns It is also possible to only insert data in specific columns. The following SQL statement will insert a new record, but only insert data in the "FIRSTNAME", "LASTNAME", and "ADDRESS" columns: But Remember other columns should not be NOT NULL columns INSERT INTO Human (FIRST_NAME,LAST_NAME,ADDRESS) VALUES ('ib','MAM','Delhi'); Populate one table using another table You can populate the data into a table through the select statement over another table; provided the other table has a set of fields, which are required to populate the first table. Here is the syntax : INSERT INTO First_Table_Name [(column1, column2, ... columnN)] SELECT column1, column2, ...columnN FROM Second_Table_Name [WHERE condition]; ---------- -------------------- -------------------- ---------- ---------------- ID FIRST_NAME LAST_NAME AGE ADDRESS ---------- -------------------- -------------------- ---------- ---------------- 1 Rumman Ansari 32 Ahmedabad 2 Musar Mondal 25 Delhi 3 Osman Sk 23 Kota 4 Badsha Roy 25 Mumbai 5 Alamgir Roy 27 Bhopal 6 Rajesh Roy 22 MP 7 Rambo Azmi 24 Indore ---------- -------------------- -------------------- ---------- ----------------
  • 31. Insert Data into Client_master_21 table Sql Query Insert Data into Product_master_21 table SQL Code Insert Data into Salesman_master_21 table ------ | ---------- --------------- | ---------- | --------------- | --------- CLIENT | NAME | PINCODE | STATE | BAL_DUE ------ | ---------- --------------- | ---------- | --------------- | --------- C00001 | Amit Saman | Kolkata | 700001 | West | 15000. | ta | | | Bengal | C00002 | Tapos Das | Mumbai | 400012 | Maharashtra | C00003 | Anup Maiti | Mumbai | 400014 | Maharashtra | 500 C00004 | Bimal Roy | Chennai | 600018 | Tamil Nadu | C00005 | Moni Kar | Kolkata | 700017 | West Bengal | 200 C00006 | AR Khan | Delhi | 700024 | Delhi | ------ | ---------- --------------- | ---------- | --------------- | --------- INSERT INTO Client_master_21 VALUES('C00001','Amit Samanta','','','Kolkata',70 INSERT INTO Client_master_21 VALUES('C00002','Tapos Das','','','Mumbai',400012 INSERT INTO Client_master_21 VALUES('C00003','Anup Maiti','','','Mumbai',40001 INSERT INTO Client_master_21 VALUES('C00004','Bimal Roy','','','Chennai',60001 INSERT INTO Client_master_21 VALUES('C00005','Moni Kar','','','Kolkata',700017 INSERT INTO Client_master_21 VALUES('C00006','AR Khan','','','Delhi',700024,'D ------ | --------------- | -------------- | ---------- | ----------- | --------- PRODUC | DESCRIPTION | PROFIT_PERCENT | UNIT_MEASU | QTY_ON_HAND | RECODE_ ------ | --------------- | -------------- | ---------- | ----------- | --------- P00001 | 1.44 | 5 | piece | 100 | 2 | floppies | | | | P03453 | 6 | 5 | piece | 10 | 2 P06734 | 5 | 5 | piece | 20 | 2 P07868 | 5 | 5 | piece | 10 | 2 P07885 | 25 | 5 | piece | 10 | 2 ------ | --------------- | -------------- | ---------- | ----------- | --------- INSERT INTO Product_master_21 VALUES('P00001','1.44 floppies',5,'piece',100,2 INSERT INTO Product_master_21 VALUES('P03453','6',5,'piece',10,20,3,11280); INSERT INTO Product_master_21 VALUES('P06734','5',5,'piece',20,20,5,100); INSERT INTO Product_master_21 VALUES('P07868','5',5,'piece',10,20,3,1000); INSERT INTO Product_master_21 VALUES('P07885','25',5,'piece',10,20,3,5100);
  • 32. ©Privacy Contact information: atnyla (http://www.atnyla.com). By: Rumman Ansari (http://www.atnyla.com/tuition). INSERT INTO Salesman_master_21 VALUES('S00001','Krim','A/4','Alipore','CityA',700012,'StateE',3000,100,50,'Go INSERT INTO Salesman_master_21 VALUES('S00002','Azad','65','Barabazar','CityB',700001,'StateF',3000,200,100,' INSERT INTO Salesman_master_21 VALUES('S00003','Samir','p- 7','Chadni','CityC',700022,'StateG',3000,200,100,'Good'); INSERT INTO Salesman_master_21 VALUES('S00004','Anindya','A/7','Saltlake','CityD',700091,'StateH',3500,500,15