Creating Tables in SQL
Creating Tables in SQL
Functions in SQL
• Functions are created for manipulating the data and returning a value.
• Functions must be created before calling them. Otherwise, you will have the following error:
• Functions are formerly created codes that reside in the database. They can be called easily by typing only their
names and parameters.
• A function is a bunch of code created for reuse.
• There are two types of functions:
1. Single-Row Functions
2. Multiple-Row Functions
• The single row functions operate on single rows and return only one result per row.
• Accept one or more arguments and return one value.
• Return value for each row returned.
• Can be used alone or nested.
• A column or an expression can be used as arguments.
• The returning data type can be different than the input data types.
• Can be used in SELECT, WHERE or ORDER BY clauses.
• Character functions take in character data as input and return character or numeric data as output
• There are two different types of character functions:
Numeric functions accept numeric values as the input and return numeric values as the
output.
• ROUND: Takes in a number and rounds it to the specified number of decimal places.
• TRUNC: Truncates values to the specified number of decimal places.
• CEIL: Returns the smallest integer number greater than or equal to a specified number.
• FLOOR: Returns the highest integer number less than or equal to a specified number.
• MOD: Returns the remainder of division.
• The RR date format is a different time format element that allows us to show 20th-century
dates in the 21st century using only 2 digits.
• DATE: The standard data type that stores date values in Oracle.
• TIMESTAMP: This data type stores year, month, day, hour, minute, second as well as
fractional seconds.
• TIMESTAMP WITH TIME ZONE: This data type is the same as TIMESTAMP, but it stores
the timezone along with it.
• TIMESTAMPT WITH LOCAL TIME ZONE: This data type is similar to TIMESTAMP WITH
TIME ZONE, but the stored timezone is the database's timezone.
• SYSDATE: returns the current date and time of the OS where the Oracle Database is installed.
• CURRENT DATE: returns the current date and time of the place where the user's session is logged
in from.
• SESSIONTIMEZONE: returns time zone of the user's session.
• SYSTIMESTAMP: returns the date and time of the database.
• CURRENT TIMESTAMP: returns current date and time from user's session.
• We can add or subtract a number from a date, and the result of this operation will also be a date.
• Subtracting two dates returns the number of days between these dates.
1. Implicit Conversion: Oracle server automatically converts some data type to the required
one.
• Although implicit conversion converts some data types automatically, it is recommended
to use the explicit conversion functions to ensure the reliability of SQL statements.
2. Explicit Conversion: Explicit conversions are done using the conversion functions explicitly.
• Used for converting data type of a value to another data type explicitly.
• There are 3 functions for explicit data type conversion which are:
1. TO_CHAR ()
2. TO_NUMBER ()
3. TO_DATE ()
• date I number: This is the value to be converted into the VARCHAR2 type. It can be a date or a
number.
• format_model: The input value is converted to the VARCHAR2 data type in the specified format
model.
• nls_parameter: Specifies the language for the names and abbreviations of the days and months
(E.g., Monday - Mon, January - Jan, etc.)
• The TO CHAR function is case-sensitive.
• Number format models are used with the TO CHAR function to display the
numeric values in different formats.
• Converts the characters to a DATE data type in the specified format model.
• If the Expression1 is not null then, it returns the Expression2. If the Expression1 is
NULL, then, the Expression3 is returned.
• And the Expression1 does not have to be the same data type in Expression2 and
Expression3. But, the Expression2 and Expression3 must be in the same data type.
• Data types can be the characters, numbers or dates.
• Compares Expression1 and Expression2. If they are equal, returns NULL. But, if they are not equal,
returns Expression1.
• The Expression1 and Expression2 must be in same data type.
• The COALESCE function accepts a list of arguments and returns the first one that
evaluates to a non-null value.
• There are two ways of using the CASE expressions which are Simple Case Expression and
Searched Case Expression.
• The expression is stated at the beginning, • The expressions are used within each
and the possible results are checked in the condition without mentioning it at the start
condition parameters. of the CASE expression.
• The DECODE is a function that is specific to Oracle, easy to use, and an alternative to the CASE
expressions.
• It is used to provide an if-then-else logic in SQL.
• The CASE is an expression whereas DECODE is a function.
• Group functions operate on multiple rows and return one result for each group.
• Group functions are usually used after the SELECT keyword.
• Multiple group functions can be used in a single SELECT statement.
• Group functions ignore the NULL values! But, you can use the NVL, NVL2, COALESCE, DECODE functions
or CASE expressions to handle the NULL values.
• The DISTINCT and ALL(default) keywords are used with the group function to consider duplicate values.
• CHAR, VARCHAR2, NUMBER or DATE data types can be used.
• You can use the DISTINCT keyword to count the distinct values of the expression, or ALL
keyword to count all rows.
• '*' represents all rows including the NULL values.
• If a column name is used with the Count function, this time, the NULL values are ignored.
• We can use the NVL, NVL2, COALESCE, CASE or DECODE functions to handle the NULL values.
• The ON DELETE CASCADE clause deletes dependent rows in the child table when a related
row in the parent table is deleted.
• The ON DELETE SET NULL clause updates dependent rows in the child table to NULL when a
related row in the parent table is deleted.
• ON DELETE CASCADE
• ON DELETE SET NULL
• To add a NOT NULL CONSTRAINT, we use the ALTER TABLE MODIFY COLUMN clause.
• The table must be empty or all the values of the NOT NULL column must have a value.
• We cannot modify a constraint's structure. To do that we need to drop and re-create the constraint.
• Specifying a constraint name is optional.
• While dropping a PRIMARY KEY constraint, we can use the CASCADE option to drop all the associated
FOREIGN KEY constraints.
• We can use the ONLINE keyword to allow DML operations while dropping constraints.
• When we drop a column, the NOT NULL, PRIMARY KEY and UNIQUE constraints are dropped.
• If a column is used by a foreign key constraint on another table, then, we cannot drop this column
directly. The Oracle Server will return an error.
• If we use the CASCADE CONSTRAINTS clause while dropping a column, all the constraints referring to
that column's PRIMARY and UNIQUE keys are dropped.
• If there is a multi-column constraint, we need to use the CASCADE CONSTRAINTS clause to drop this
column. Otherwise, we will get an error.
• The ENABLE keyword ensures that all existing and new data must meet the constraint conditions.
• The DISABLE keyword allows us to enter new values whether they meet the constraint conditions or not.
• VALIDATE or NOVALIDATE can also be used with ENABLE and DISABLE for configuring existing or future data validation.
• The VALIDATE keyword ensures that the existing data must conform to the constraint conditions.
• The NOVALIDATE keyword is used to suspend the checking of existing rows for constraint violations.
• A constraint can have four states below:
1. ENABLE VALIDATE: This is the default for the ENABLE clause. All the existing rows are checked for whether they
conform to the constraint rules, plus, all future rows that will be added or modified will be checked while making
inserts or updates.
2. ENABLE NOVALIDATE: The existing rows will not be checked. Only the new rows will be checked.
3. DISABLE VALIDATE: The constraint is disabled, but it remains valid. No DML operations are allowed on the table
because the future changes will not be validated.
4. DISABLE NOVALIDATE: This is the default for the DISABLE clause. No checks are performed on existing and future
data.
• Constraints can have 2 deferring states which are DEFERRABLE and NOT DEFERRABLE.
1. DEFERRABLE: Allows us the option of postponing enforcement of constraints to the end of a
transaction. A deferrable constraint can be created in one of two modes:
a) INITIALLY IMMEDIATE (DEFAULT): Specifies that the constraint are checked for conformance
at the end of each queryexecution.
b) INITIALLY DEFERRED: Postpones constraint enforcement until the end of the transaction
(commit).
2. NOT DEFERRABLE (DEFAULT): Indicates the enforcement of the constraint must always be done
immediately as each statement is executed.
• If we want to alter a constraint or all the constraints in a session to DEFERRED, he NOT DEFERRABLE
constraints will not change to DEFERRED.