Oracle Datatypes and Creating Tables
A table is composed of rows and columns.
A table in Oracle Ver. 7.3 can have maximum 255 Columns and in Oracle Ver. 8 and above a table can have maximum 1000 columns. Number of rows in a table is unlimited in all the versions.
A table can represent a single entity that you want to track within your system. This type of a table could represent a list of the employees within your organization, or the orders placed for your company's products.
A table can also represent a relationship between two entities. This type of a table could portray the association between employees and their job skills, or the relationship of products to orders. Within the tables, foreign keys are used to represent relationships.
Although some well designed tables could represent both an entity and describe the relationship between that entity and another entity, most tables should represent either an entity or a relationship.
The following sessions explain how to create, alter, and drop tables. Some simple guidelines to follow when managing tables in your database are included.
Designing Tables
Consider the following guidelines when designing your tables:
- Use descriptive names for tables, columns, indexes, and clusters.
- Table Names, Columns Names can contain maximum of 30 characters and they should start with an alphabet.
- Be consistent in abbreviations and in the use of singular and plural forms of table names and columns.
- Select the appropriate datatype for each column.
- Arrange columns that can contain NULL Values in the last, to save storage space.
Before creating a table, you should also determine whether to use integrity constraints. Integrity constraints can be defined on the columns of a table to enforce the business rules of your database automatically.
Before creating a Table you also have to decide what type of data each column can contain. This is known as datatype. Lets Discuss what datatypes are available in Oracle.
Datatypes
A datatype associates a fixed set of properties with the values that can be used in a column of a table or in an argument of a procedure or function. These properties cause Oracle to treat values of one datatype differently from values of another datatype. For example, Oracle can add values of NUMBER datatype, but not values of RAW datatype.
Oracle supplies the following built-in datatypes:
Character datatypes
Numeric datatypes
Oracle CREATE TABLE
In Oracle, CREATE TABLE statement is used to create a new table in the database.
To create a table, you have to name that table and define its columns and datatype for each column.
Syntax:
Parameters used in syntax
- table_name: It specifies the name of the table which you want to create.
- column1, column2, ... column n: It specifies the columns which you want to add in the table. Every column must have a datatype. Every column should either be defined as "NULL" or "NOT NULL". In the case, the value is left blank; it is treated as "NULL" as default.
Oracle CREATE TABLE Example
Here we are creating a table named customers. This table doesn't have any primary key.
This table contains three columns
- customer_id: It is the first column created as a number datatype (maximum 10 digits in length) and cannot contain null values.
- customer_name: it is the second column created as a varchar2 datatype (50 maximum characters in length) and cannot contain null values.
- city: This is the third column created as a varchar2 datatype. It can contain null values.
Oracle CREATE TABLE Example with primary key
What is Primary key
A primary key is a single field or combination of fields that contains a unique record. It must be filled. None of the field of primary key can contain a null value. A table can have only one primary key.
Oracle Insert Statement
In Oracle, INSERT statement is used to add a single record or multiple records into the table.
Syntax: (Inserting a single record using the Values keyword):
Syntax: (Inserting multiple records using a SELECT statement):
Parameters:
1) table: The table to insert the records into.
2) column1, column2, ... column_n:
The columns in the table to insert values.
3) expression1, expression2, ... expression_n:
The values to assign to the columns in the table. So column1 would be assigned the value of expression1, column2 would be assigned the value of expression2, and so on.
4) source_table:
The source table when inserting data from another table.
5) conditions:
The conditions that must be met for the records to be inserted.
Oracle Insert Example: By VALUE keyword
It is the simplest way to insert elements to a database by using VALUE keyword.
See this example:
Consider here the already created suppliers table. Add a new row where the value of supplier_id is 23 and supplier_name is Flipkart.
See this example:
Output:
1 row(s) inserted. 0.02 seconds
Oracle Insert Example: By SELECT statement
This method is used for more complicated cases of insertion. In this method insertion is done by SELECT statement. This method is used to insert multiple elements.
See this example:
In this method, we insert values to the "suppliers" table from "customers" table. Both tables are already created with their respective columns.
Execute this query:
Output:
4 row(s) inserted. 0.00 seconds
You can even check the number of rows that you want to insert by following statement:
Output:
Count(*) 4
Oracle UPDATE Statement
In Oracle, UPDATE statement is used to update the existing records in a table. You can update a table in 2 ways.Traditional Update table method
Syntax:Update Table by selecting rocords from another table
Syntax:Parameters:
1) column1, column2, ... column_n:It specifies the columns that you want to update.2) expression1, expression2, ...expression_n:This specifies the values to assign to the column1, column2, ?. column_n.3) conditions:It specifies the conditions that must be fulfilled for execution of UPDATE stateme.Oracle Update Example: (Update single column)
This example will update the supplier_name as "Kingfisher" where "supplier_id" is 2.Oracle Update Example: (Update multiple columns)
The following example specifies how to update multiple columns in a table. In this example, two columns supplier_name and supplier_address is updated by a single statement.Output:1 row(s) updated. 0.06 secondsOracle Update Example: (By selecting records from another table)
Output:2 row(s) updated. 0.02 secondsOracle DELETE Statement
In Oracle, DELETE statement is used to remove or delete a single record or multiple records from a table.SyntaxParameters
1) table_name: It specifies the table which you want to delete.2) conditions: It specifies the conditions that must met for the records to be deleted.Oracle Delete Example: On one condition
This statement will delete all records from the customer table where name is "Sohan".Oracle Delete Example: On multiple conditions
This statement will delete all records from the customers table where the last_name is "Maurya" and the customer_id is greater than 2.Oracle TRUNCATE TABLE
In Oracle, TRUNCATE TABLE statement is used to remove all records from a table. It works same as DELETE statement but without specifying a WHERE clause. It is generally used when you don?t have to worry about rolling backOnce a table is truncated, it can?t be rolled back. The TRUNCATE TABLE statement does not affect any of the table?s indexes, triggers or dependencies.SyntaxParameters
1) schema_name: This parameter specifies the name of the schema that the table belongs to. It is optional.2) table_name: It specifies the table that you want to truncate.Oracle TRUNCATE Table Example
Consider a table named "customers" and execute the following query to truncate thisOutputTable truncated. 1.11 secondsNow check the customers table, you will find that there is no data available in that table. It is equally similar to DELETE TABLE statement in Oracle.Oracle DELETE Table Example
TRUNCATE TABLE vs DELETE TABLE
Both the statements will remove the data from the "customers" table but the main difference is that you can roll back the DELETE statement whereas you can't roll back the TRUNCATE TABLE statement.Oracle ALTER TABLE Statement
In Oracle, ALTER TABLE statement specifies how to add, modify, drop or delete columns in a table. It is also used to rename atable.How to add column in a table
Syntax:Example:Consider that already existing table customers. Now, add a new column customer_age into the table customers.Now, a new column "customer_age" will be added in customers table.How to add multiple columns in the existing table
Syntax:ExampleNow, two columns customer_type and customer_address will be added in the table customers.How to modify column of a table
Syntax:Example:Now the column column_name in the customers table is modified to varchar2 (100) and forced the column to not allow null values.How to modify multiple columns of a table
Syntax:Example:This will modify both the customer_name and city columns in the table.How to drop column of a table
Syntax:Example:This will drop the customer_name column from the table.How to rename column of a table
Syntax:Example:This will rename the column customer_name into cname.How to rename table
Syntax:Example:This will rename the customer table into "retailers" table.
Oracle DROP TABLE Statement
Oracle DROP TABLE statement is used to remove or delete a table from the Oracle database.
Syntax
Parameters
schema_name: It specifies the name of the schema that owns the table.
table_name: It specifies the name of the table which you want to remove from the Oracle database.
CASCADE CONSTRAINTS: It is optional. If specified, it will drop all referential integrity constraints as well.
PURGE: It is also optional. If specified, the table and its dependent objects are placed in the recycle bin and can?t be recovered.
If there are referential integrity constraints on table_name and you do not specify the CASCADE CONSTRAINTS option, the DROP TABLE statement will return an error and Oracle will not drop the table.
DROP TABLE Example
This will drop the table named customers.DROP TABLE Example with PURGE parameter
This statement will drop the table called customers and issue a PURGE so that the space associated with the customers table is released and the customers table is not placed in recycle bin. So, it is not possible to recover that table if required.
Oracle DISTINCT Clause
Oracle DISTINCT clause is used to remove the duplicate records from the result set. It is only used with SELECT statement.
Syntax:
Parameters:
expressions:It specifies the columns that you want to retrieve.
tables: It specifies the table from where you want to retrieve records.
conditions: It specifies the conditions that must be fulfilled.
Oracle DISTINCT Example: (with single expression)
Let's take a table "customers"
Customer table:
Execute this query:
Output:
Oracle DISTINCT Example: (with multiple expressions)
Execute this query:
Output:
This example specifies distinct name, age and salary of the customer where age is greater than or equal to 65.
Oracle FROM Clause
FROM clause is a mandatory clause in SELECT expression. It specifies the tables from which data is to be retrieved.
Syntax:
Oracle FROM Clause Example: (with one table)
Let's take an example to explain how to use FROM clause to retrieve data from one table. Consider a table "customers".
Customer table:
Execute this query:
Output:
Oracle FROM Clause Example: (with two tables)
Inner Join example:
Let's take two tables "suppliers" and "order1".
Suppliers:
Order1:
Execute the following query:
Output:
Oracle ORDER BY Clause
In Oracle, ORDER BY Clause is used to sort or re-arrange the records in the result set. The ORDER BY clause is only used with SELECT statement.
Syntax:
Parameters:
expressions: It specifies columns that you want to retrieve.
tables: It specifies the table name from where you want to retrieve records.
conditions: It specifies the conditions that must be fulfilled for the records to be selected.
ASC: It is an optional parameter that is used to sort records in ascending order.
DESC: It is also an optional parameter that is used to sort records in descending order.
Oracle ORDER BY Example: (without ASC/DESC attribute)
Let's take a table "supplier"
Supplier table:
Execute this Query:
Output:
The above example returns the first_name ordered by last_name in ascending order.
Oracle ORDER BY Example: (sorting in descending order)
If you want to sort your result in descending order, you should use the DESC attribute in your ORDER BY clause:
Execute this Query:
Output
The above example returns the first_name ordered by last_name in descending order.
Oracle GROUP BY Clause
In Oracle GROUP BY clause is used with SELECT statement to collect data from multiple records and group the results by one or more columns.
Syntax:
Parameters:
expression1, expression2, ... expression_n: It specifies the expressions that are not encapsulated within aggregate function. These expressions must be included in GROUP BY clause.
aggregate_function: It specifies the aggregate functions i.e. SUM, COUNT, MIN, MAX or AVG functions.
aggregate_expression: It specifies the column or expression on that the aggregate function is based on.
tables: It specifies the table from where you want to retrieve records.
conditions: It specifies the conditions that must be fulfilled for the record to be selected.
Oracle GROUP BY Example: (with SUM function)
Let's take a table "salesdepartment"
Salesdepartment table:
Execute this query:
Output
The above example will show the total sales of every individual item.
Oracle GROUP BY Example: (with COUNT function)
Let's take a table "customers"
Here we are creating a table named customers. This table doesn't have any primary key.
Customer table:
Execute this query:
Output:
Oracle GROUP BY Example: (with MIN function)
Let?s take a table "employees"
Employees table:
Execute this query:
Output:
Oracle GROUP BY Example: (with MAX function)
In this example, we are using "employees" table that is given above.
Execute this query:
Output:
Oracle HAVING Clause
In Oracle, HAVING Clause is used with GROUP BY Clause to restrict the groups of returned rows where condition is TRUE.
Syntax:
Parameters:
expression1, expression2, ... expression_n: It specifies the expressions that are not encapsulated within aggregate function. These expressions must be included in GROUP BY clause.
aggregate_function: It specifies the aggregate functions i.e. SUM, COUNT, MIN, MAX or AVG functions.
aggregate_expression: It specifies the column or expression on that the aggregate function is based on.
tables: It specifies the table from where you want to retrieve records.
conditions: It specifies the conditions that must be fulfilled for the record to be selected.
having_conditions: It specifies the conditions that are applied only to the aggregated results to restrict the groups of returned rows.
Oracle HAVING Example: (with GROUP BY SUM function)
Let's take a table "salesdepartment"
Salesdepartment table:
Execute this query:
Output:
Oracle HAVING Example: (with GROUP BY COUNT function)
Let's take a table "customers"
Customer table:
Execute this query:
Output:
Oracle HAVING Example: (with GROUP BY MIN function)
Let's take a table "employees"
Employees table:
Execute this query:
Output
Oracle HAVING Example: (with GROUP BY MAX function)
Execute this query:
Output:
Oracle Joins
Join is a query that is used to combine rows from two or more tables, views, or materialized views. It retrieves data from multiple tables and creates a new table.
Join Conditions
There may be at least one join condition either in the FROM clause or in the WHERE clause for joining two tables. It compares two columns from different tables and combines pair of rows, each containing one row from each table, for which join condition is true.
Types of Joins
- Inner Joins (Simple Join)
- Outer Joins
- Equijoins
- Self Joins
- Cross Joins (Cartesian Products)
- Antijoins
- Semijoins
Oracle INNER JOIN
Inner Join is the simplest and most common type of join. It is also known as simple join. It returns all rows from multiple tables where the join condition is met.
Syntax
Image representation of Inner Join
Oracle INNER JOIN Example
Let's take an example to perform Inner Join on two tables "Suppliers" and "Order1".
Suppliers
Order1
This example will return all rows from "suppliers" and "order1" table where there is a matching supplier_id value in both the suppliers and order1 tables.
Execute the following query
Output
Oracle OUTER JOIN
An outer join is similar to equijoin but it gets also the non-matched rows from the table. It is categorized in Left Outer Join, Right Outer Join and Full Outer Join by Oracle 9i ANSI/ISO 1999 standard.
Left Outer Join
Left Outer Join returns all rows from the left (first) table specified in the ON condition and only those rows from the right (second) table where the join condition is met.
Syntax
Image representation of left outer join
Example
In this example, we are performing left outer join on the already created tables ?suppliers? and ?order1?.
The following example would return all records from table ?suppliers? and only those records from table ?order1? where the joinfields are equal.
Execute this query
Output
Right Outer Join
The Right Outer Join returns all rows from the right-hand table specified in the ON condition and only those rows from the othertable where the join condition is met.
Syntax
Image representation of Right Outer Join
Example
In this example, we are performing right outer join on the already created tables ?suppliers? and ?order1?.
The following example would return all rows from the order1 table and only those rows from the suppliers table where the joincondition is met.
Execute this query
Output
Full Outer Join
The Full Outer Join returns all rows from the left hand table and right hand table. It places NULL where the join condition is not met.
Syntax
Image representation of Full Outer Join
Example
In this example, we are performing full outer join on the already created tables ?suppliers? and ?order1?.
The following example will return all rows from the ?suppliers? table and all rows from the ?order1? table and whenever the joincondition is not met, it places the NULL value.
Execute this query
Output
No comments:
Post a Comment