Create Table In Oracle

CREATING TABLE IN ORACLE DATABASE: -  Table is the combination of rows and columns, it is object of user or schema. In database the data is stored in the form of Tables.
To create table in Oracle: -
Syntax: -
CREATE TABLE <tablename>
( colname datatype (size),
colname datatype (size),
---------------------);
Rules that are followed while creating a table: -
○ Table name should start only with Alphabets, and the name of the table should not contain spaces and special symbols or characters except _, $, # these three can be  used in table names.
Examples for table name:-
Emp 123 (Invalid)
Emp*123 (Invalid)
Emp_123 (Valid)
Emp$123 (Valid)
Emp#123 (Valid)
○ Table name can be maximum of 30 characters, a table can have maximum of 1000 columns and number of rows are unlimited.
Example: -
Creating a table with following structure: -
Table name EMP
EMPNO
ENAME
JOB
SAL
HIREDATE
SQL>
CREATE TABLE EMPL
(EMPNO NUMBER (4),
ENAME VARCHAR2 (10),
JOB VARCHAR2 (9),
SAL NUMBER (7, 2),
HIREDATE DATE);
Above command creates table structure/ definition of table/ metadata that includes column names, data type and size.
DESCRIBE COMMAND: - It is a command used to see the table definition or structure or metadata. It is SQL PLUS command.

 DIFFERENCE BETWEEN SQL COMMANDS AND SQL PLUS COMMANDS

SQL COMMANDS
SQL PLUS COMMANDS
These are used to perform operations over database.
These are used to set the environment

It must be terminated with ‘;’.
‘;’ is not required.
These commands cannot be Abbreviated.
These can be Abbreviated.
Syntax for Describe command: -
DESC  <tablename>
Table and it's Defintion
INSERTING DATA INTO TABLE: -
“INSERT” command is used to insert data into table.
Syntax: -
INSERT INTO <tablename>
VALUES(V1, V2, V3, V4, ……….);
Strings and date’s must be enclosed in single quotes ‘’
Example: -
INSERT INTO EMPL
VALUES (100, ‘SACHIN’, ‘MANAGER’, 10000, ’26-AUG-16’);
Inserted Rows
INSERT INTO EMPL
VALUES (101, ‘SHRI’, ‘ACCOUNTANT’, 8000, SYSDATE);
In the above row we wrote sysdate instead of date, with that function we will get the system date and that sysdate should not be mentioned in single quotes ‘’. 
Row with Sysdate
If we place sysdate in single quotes then oracle consider it as string and it will display’s the text as sysdate.
Share:

Oracle Data Types

DATA TYPES IN ORACLE
A data type specifies type of the data allowed in a column and also amount of memory allocated for that column.
CHAR (Size): - It allows character data and it is recommended only for fixed length columns.
EXAMPLE: -
Name char(20)
S
A
C
H
I
N















H
A
R
I

















In the above example there is memory wastage, so don’t use char data type for variable length fields like Name, place, job so on. Use char data type only for fixed length columns.
EXAMPLES FOR FIXED LENGTH COLUMNS:
○ State Code Char (2)
AP
TS
MH
TN
KA
○ Country Code Char (3)
IND
USA
ENG
○ Gender Char (1)
M
F
Char Data Type

VARCHAR2: -
It allows character data up to 4000 characters in Oracle11g and 32767 characters in Oracle12c. Varchar2 is recommended for variable length character fields.
EXAMPLE: -
Name Varchar2 (20)
S
A
C
H
I
N















H
A
R
I

















In the above example up to the name it will consumes the memory and rest unused bytes it releases, that bytes can be used by other names or other data. In char data type it won’t release the unused bytes.
Up to Oracle 6 version it is Varchar this is ANSI data type. From Oracle 7 version it changed to Varchar2 this is ORACLE own data type. Functionality wise both are same.

CHAR, VARCHAR2 allows Alphanumeric (Both Characters and Numbers) data.

NUMBER (P): - It allows numeric data up to 38 digits.
EXAMPLE: -
Empno Number (4)
100 is allowed
1000 is allowed
10000 it is not allowed, as the digits are more than 4
NUMBER (P,S): - It allows numbers with decimals.
P --- Precision --- Total no of digits allowed
S --- Scale --- Number of digits allowed after decimal.
EXAMPLE: -
Sal Number (7, 2) (Before decimal 5digits after decimal 2digits total 7)
5000 is allowed
5000.50 is allowed
50000.50 is allowed
500000.50 is not allowed
5000.507 --- allowed, it will be rounded to one, oracle stores like this (5000.51)
5000.503 --- allowed, it will be rounded as 5000.50
DATE: -
It allows date and time, but time is optional, if not entered Oracle stores 12.00AM. Default date format is DD-MON-YY or DD-MON-YYYY.
EXAMPLE: -
Hiredate  Date
25-Aug-16
25-Aug-2016.
Date occupies 7 bytes             0 – 255 It is 1byte
Input: - DD-MON-YYYY                256 – 32767 It is 2bytes
DB: - DD MM YYYY HH MI SS
            1      1       2         1    1    1
TIMESTAMP: -
It allows date, time and milliseconds
EXAMPLE: -
T          Timestamp
DD-MON-YYYY HH:MI:SS.FFF
25-Aug-2016   11:40:20.1234
LOB’S: - Large Objects, introduced in Oracle 8 version, it is of 3 types
BLOB: - Binary Lob
BFILE: - Binary File Lob
CLOB: - Character Lob
BLOB/BFILE: - Both are used for storing binary data that includes audio, video and images up to 4GB.
If we use BFILE the original picture is stored outside the database but database stores path.
BFILE

If we use BLOB the picture is stored inside the database.
BLOB


CLOB: - It allows character data and character object up to 4GB.
Share:

Creating User in Oracle

Creating User / Schema / Account in Oracle Database
○ Only DBA (Database Administrator) can create new user. While installing Oracle software it asks a password for admin system. So here the database administrator is SYSTEM and password which we provided at the time of installation.
(Username: SYSTEM and Password: MANAGER)
Syntax: -
CREATE USER <NAME> IDENTIFIED BY <PASSWORD>
To create user connect as DBA
Username: - SYSTEM/MANAGER
SQL> (we will get sql prompt like this)
EXAMPLE: -
CREATE USER BATCH6PM IDENTIFIED BY ORACLE;
After executing above command a user BATCH6PM is created in database but it is not having permissions to do any operations.
Give the permissions to the user by using “GRANT” command after creating user.
EXAMPLE: -
GRANT CONNECT, RESOURCE TO BATCH6PM;
CONNECT: - To connect to Database
RESOURCE: - To create objects like Tables
(If we give both connect and resource permission to any user that will be a normal user. Normal user will not have any other access to do instead of changing password for his own user or schema)
DBA: - It means giving all permissions along with connect and resource (If we give this then the user will become Administrator).
To test with which user we are logged in the command is
SHOW USER
CHANGING PASSWORD: -
WHO CAN CHANGE PASSWORD?
 ○ Both user and Database Administrator can change password
BY USER (BATCH6PM/ORACLE)
SQL> PASSWORD
Changing password for BATCH6PM
Old Password:                           (It will not display the password even though we type)
New Password:                          (It will not display the password even though we type)
Retype Password:                      (It will not display the password even though we type)
Password changed
SQL>
BY DBA (SYSTEM/MANAGER)
SQL> ALTER USER BATCH6PM IDENTIFIED BY ORACLE12C;
DROPING USER: Only DBA can drop the user
SQL> DROP USER <NAME> [CASCADE]
Here cascade is optional, anything which is placed in the square brackets will be optional
If user is empty means without any objects then the CASCADE is not required.
If user in not empty means having objects like TABLES or VIEWS then CASCADE is required.
EXAMPLE OF DROPING USER:

DROP USER BATCH6PM;
Share:

SQL

SQL
Structured Query Language used to communicate with Oracle server, user communicates with Oracle server by sending commands/ instructions called queries.
A query is command or instruction submitted to Oracle server by the user to perform some operation over database.
This SQL language is introduced by IBM and initial name of this language was “SEQUEL” and later it is renamed to SQL. It is now under control of ANSI.
SQL is common to all Databases.
Based on operations over database SQL is categorized into following sub languages:
DDL: - Data Definition Language
DML: - Data Manipulation Language
DQL: - Data Query Language
TCL: - Transaction Control Language
DCL: - Data Control Language
Data & Definition: - Facts about person’s, places, events are called as data.
Ex: - 1, Shri, 5000
○ Facts are described by definition that is known as data definition or Meta data.
Meta data: - Data  about Data
Data about data
DDL: - Set of commands to perform operations over data definition.

DDL COMMANDS
  DML: - Set of commands to perform operations over data.
DML COMMANDS
DQL: - Command used to get data from database.

            SELECT

TCL: - Set of commands to control transactions (dual operations).
TCL COMMANDS
DCL: - Set of commands to control data.
DCL COMMANDS
Tools

Used to execute SQL commands are:

SQL PLUS / TOAD / SQL DEVELOPER

○ SQL commands are not case sensitive.
○ SQL commands must be terminated with ' ; '

Share:

Oracle 11g 12c

General Information About Oracle

Oracle is a brand it has Databases, Application Development Tools, ERP’s (Enterprise Resource Planning Tools), Data Warehouse and so on.
Below are examples for Database, Application Development Tools, ERP’s and Data Warehouse Tools
Database’s
Application Development Tools
ERP (Enterprise Resource Planning Tools)
Data warehouse Tools
Oracle 9i/10g/11g/12c
Java SE
Oracle EBS (Enterprise Business Solution)
OBIEE (Oracle Business Enterprise Edition)
Mysql
Java EE
Oracle Fusion
ODI (Oracle Data Integrator)

ADF (Application Development Framework)



Oracel 11g/12c it is a Database software or Back End Tool. This is available in different editions and versions

Editions:-
Enterprise Edition (Licensed):- It is a licensed source one has to purchase this edition. It supports all the features of Oracle.
Express Edition (Open source):- It is a open source any one can download and install this. It doesn’t support some features like Flashback, Partitioning etc.,
Versions:-
 The versions of oracle are 1,2,3,4,5,6,7,8,8i,9i,10g,11g and now it is 12c
Here
 “ i ” refers to Internet
“ g ” refers to Grid
“ c ” refers to Cloud
From oracle version 8 onwards it is capable of storing Audio, video, images and oracle became best database for internet applications, so in the next version suffix “ i “ is added.

Before <9i version it was single server. If you see the diagrammatic representation of below 9i version, it was like this.
single server for a database

Greater than or equal to 9i (>=9i) a grid concept was introduced in “ 9i ”, from oracle “ 9i ” onwards database can be accessed through multiple servers (Grid), Even if one server is fail database can be accessed through another server.
Multiple server's for a database

The grid version mainly used by the medium and large scale companies for small company’s single server is enough to run business. Grid version means multiple servers this is expensive and small company’s will not bear.
    If any of the company asks for Grid version the developers first asks how many nodes the client is needed, here nodes means servers.
   Till the grid version the method of installation was one method that is ON PREMISES. ON PREMISES is the method of installing software where the developers will go to the client company and installs the software in their system on premises. 
Cloud:-
From Oracle 12 onwards Oracle supports cloud, Oracle database can be added to cloud, users connect to the cloud and access databases.
Cloud
In cloud there are 2 methods of installing Oracle software in client company. One is ON PREMISES and the other is ON CLOUD.
   As we seen what is ON PREMISES in above grid version. ON CLOUD means Oracle software is purchased by a third party. That third party offers company to perform its operations with that software. 
   This will reduces the cost for the company, instead of purchasing it pay's some amount to the third party which offers the software.
   Oracle supports SaaS (Software as Service).
After Oracle Installation
After installing oracle the following components are installed in system.
○ Database
○ Instance
○ Tool (Client)
Database:-  A sample database is created.
If it is Express Edition XE will be created.
If it is Enterprise Edition ORCL will be created.
◘ Database is created in system Hard disk(Permanent Memory). It is created with following files
            • DBF (Data base file)
            • Redolog files
            • Control files
DBF it means Data base file, these are mainly used for backup. The data stored in the DBF.
Database cannot be accessed directly.
Instance:- Database is accessed through instance, Instance is created in RAM (Random Access Memory) which is a temporary memory.
Instance with Database

The system in which both instance and database are resided that system is called as Oracle server.
Client:- It is tool which helps to connect Oracle server.
○ Using client tool, Users
• Can connect to Oracle Server
• Can submit requests
• Receive response from server
Different Client Tools are:-
○ SQL Plus
○ SQL Developet
○ TOAD
○ RUN SQL Command Line
user client and server
SCHEMA:- A user in oracle database is called schema. The architecture of database, schemas and objects will be like
DATABASE
            USERS (Schema)
                        OBJECTS (Schema Objects)
List of schema objects in oracle are:
○ Tables
○ Views
○ Synonyms
○ Sequences
○ Indexes
○ Clusters
○ Materialized Views
○ Procedures
○ Functions
○ Packages
○ Triggers

Share:

Popular Posts