Implicit Data Type Conversion: Query-1
Implicit Data Type Conversion: Query-1
Query-2
SELECT employee_id,first_name,salary
FROM employees
WHERE salary > '15000';
EXPLICIT DATA TYPE CONVERSION
SQL Conversion functions are single row functions which are capable of typecasting
column value, literal or an expression.
TO_CHAR, TO_NUMBER and TO_DATE are the three functions which perform
cross modification of data types.
TO_CHAR function
TO_CHAR function is used to typecast a numeric or date input to character type with
a format model (optional).
Syntax
TO_CHAR(number1, [format], [nls_parameter])
The query format the HIRE_DATE and SALARY columns of EMPLOYEES table
using TO_CHAR function.
SELECT first_name,
TO_CHAR (hire_date, 'MONTH DD, YYYY') HIRE_DATE,
TO_CHAR (salary, '$99999.99') Salary
FROM employees
WHERE rownum < 5;
The first TO_CHAR is used to convert the hire date to the date format MONTH DD,
YYYY i.e. month spelled out and padded with spaces, followed by the two-digit day
of the month, and then the four-digit year. If you prefer displaying the month name in
mixed case (that is, "December"), simply use this case in the format argument: ('Month
DD, YYYY').
The second TO_CHAR function in Figure 10-39 is used to format the SALARY to
display the currency sign and two decimal positions.
Oracle offers comprehensive set of format models. The below table shows the list of
format models which can be used to typecast date and number values as character using
TO_CHAR.
Format Description
Model
,(comma) It returns a comma in the specified position. You can specify
multiple commas in a number format model. Restrictions:A
comma element cannot begin a number format model. A comma
cannot appear to the right of a decimal character or period in a
number format model.
TO_NUMBER function
The TO_NUMBER function converts a character value to a numeric datatype. If the
string being converted contains nonnumeric characters, the function returns an error.
Syntax
TO_NUMBER (string1, [format], [nls_parameter])
The below table shows the list of format models which can be used to typecast
character values as number using TO_NUMBER.
Format Description
Model
CC Century
AM, PM AM or PM
TO_NUMBER('1210.73','9999.9')
------------------------------
1210.7
TO_DATE function
The function takes character values as input and returns formatted date equivalent of
the same. The TO_DATE function allows users to enter a date in any format, and then
it converts the entry into the default format used by Oracle 11g.
Syntax:
TO_DATE( string1, [ format_mask ], [ nls_language ] )
Format Description
Model
WW Week of year (1-53) where week 1 starts on the first day of the
year and continues to the seventh day of the year.
W Week of month (1-5) where week 1 starts on the first day of the
month and ends on the seventh.
Scalar Functions
Aggregate Functions
Scalar Functions: Which operators on single value and returns single value, below is
the list of some scale functions used in sql server.
round (9.56785) This will round the give number to 3 places of decimal, 9.567
rand (8) This will generate random numbers between 0 and 0.8
rand( ) This will generate random number between 0 and 1
upper (‘sql’) This will return upper case of given string, ‘SQL’
lower (‘SQL’) This will return lower case of given string, ‘sql’
abs (-20.75) This will return absolute number of a given number, 20.75
Convert (int, 20.56) This will convert given float value to integer, 20
ltrim (‘ sql’) This will remove the spaces from left hand side, ‘sql’
rtrim (‘sql ‘) This will remove the spaces from right hand side, ‘sql’
ASCII (char_Exp) This will return ASCII code of the given character expression
Aggregate Functions: Aggregates the values and return a single value, below is the list
of some aggregate values in sql server.
OR REPLACE
Allows you to recreate the synonym (if it already exists) without having to
issue a DROP synonym command.
PUBLIC
It means that the synonym is a public synonym and is accessible to all users.
Remember though that the user must first have the appropriate privileges to the
object to use the synonym.
schema
The appropriate schema. If this phrase is omitted, Oracle assumes that you are
referring to your own schema.
object_name
The name of the object for which you are creating the synonym. It can be one
of the following:
table
view
sequence
stored procedure
function
package
materialized view
java class schema object
user-defined object
synonym
Example
Let's look at an example of how to create a synonym in Oracle.
For example:
SELECT *
FROM suppliers;
If this synonym already existed and you wanted to redefine it, you could always use
the OR REPLACE phrase as follows:
Drop synonym
Once a synonym has been created in Oracle, you might at some point need to drop the
synonym.
Syntax
The syntax to drop a synonym in Oracle is:
PUBLIC
Allows you to drop a public synonym. If you have specified PUBLIC, then you
don't specify a schema.
force
It will force Oracle to drop the synonym even if it has dependencies. It is
probably not a good idea to use force as it can cause invalidation of Oracle
objects.
Example
Let's look at an example of how to drop a synonym in Oracle.
For example:
This DROP statement would drop the synonym called suppliers that we defined
earlier
MODIFYING A VIEW
ALTER VIEW - change the definition of an existing view
ALTER VIEW uses the exact same syntax as CREATE VIEW and will succeed only
if the view already exists.
SYNTAX:
ALTER VIEW view-name [(column-commalist)] AS query
EXAMPLE:
ALTER VIEW MyView (MyViewCol1,MyViewCol2,MyViewCol3) AS
SELECT TableCol1, TableCol2, TableCol3 FROM MyTable
DROP VIEW
The DROP VIEW command removes a view, but does not removes the underlying
tables or data.
SYNTAX :
DROP VIEW view-name
EXAMPLE :
DROP VIEW MyView
SUB QUERIES
A subquery is a SQL query nested inside a larger query.
A subquery may occur in :
o - A SELECT clause
o - A FROM clause
o - A WHERE clause
The subquery can be nested inside a SELECT, INSERT, UPDATE, or
DELETE statement or inside another subquery.
A subquery is usually added within the WHERE Clause of another SQL
SELECT statement.
You can use the comparison operators, such as >, <, or =. The comparison
operator can also be a multiple-row operator, such as IN, ANY, or ALL.
A subquery is also called an inner query or inner select, while the statement
containing a subquery is also called an outer query or outer select.
The inner query executes first before its parent query so that the results of an
inner query can be passed to the outer query.
You can use a subquery in a SELECT, INSERT, DELETE, or UPDATE statement to
perform the following tasks:
Compare an expression to the result of the query.
Determine if an expression is included in the results of the query.
Check whether the query selects any rows.
Look at Chris entry. He has only subject. Hence Subject2 for him is NULL.
Here storage space for second entry is simply wasted.
In the case of Joseph, he has two subjects, Mathematics and Physics, both the
columns have values. Imagine if he opts for third subject? There is no
column for his third entry. In this case, whole table needs to be altered,
which is not good at this stage. Once database is designed, it should be a
perfect one. We should not be modifying it as we start adding/updating data.
One of the requirements of 1NF is, each table should have primary key. This
key in the table makes each record unique. In our example we have it
already- STUDENT_ID.
Here, SUBJECT1 and SUBJECT2 are same set of columns, i.e.; it has same
kind of information stored - Subject, which is a violation of first rule of 1NF.
As it states, there should not be any repeating columns. We have to remove
such columns. But think how?
In order to have STUDENT in 1NF, we have to remove multiple SUBJECT columns
from STUDENT table. Instead, create only one SUBJECT column, and for each
STUDENT enters as many rows as SUBJECT he has. After making this change, the
above table will change as follows:
Now STUDENT_ID alone cannot be a primary key, because it does not uniquely
determines each record in the table. If we want to records for Joseph, and we query by
his ID,100, gives us two records. Hence Student_ID is no more a primary key. When
we observe the data in the table, all the four field uniquely determines each record.
Hence all four fields together considered as primary key.
Thus, the above table is in 1NF form.
Transitive dependency – If A->B and B->C are two FDs then A->C is called
transitive dependency.
Example 1 – In relation STUDENT given in Table 4,
FD set: {STUD_NO -> STUD_NAME, STUD_NO -> STUD_STATE,
STUD_STATE -> STUD_COUNTRY, STUD_NO -> STUD_AGE,
STUD_STATE -> STUD_COUNTRY}
Candidate Key: {STUD_NO}
For this relation in table 4, STUD_NO -> STUD_STATE and STUD_STATE ->
STUD_COUNTRY are true. So STUD_COUNTRY is transitively dependent on
STUD_NO. It violates third normal form. To convert it in third normal form, we
will decompose the relation
STUDENT (STUD_NO, STUD_NAME, STUD_PHONE, STUD_STATE,
STUD_COUNTRY_STUD_AGE) as:
As you can see, we have also added some sample data to the table.
In the table above:
One student can enrol for multiple subjects. For example, student
with student_id 101, has opted for subjects - Java & C++
For each subject, a professor is assigned to the student.
And, there can be multiple professors teaching one subject like we have for Java.
What do you think should be the Primary Key?
Well, in the table above student_id, subject together form the primary key, because
using student_id and subject, we can find all the columns of the table.
One more important point to note here is, one professor teaches only one subject, but
one subject may have two different professors.
Hence, there is a dependency between subject and professor here,
where subject depends on the professor name.
This table satisfies the 1st Normal form because all the values are atomic, column
names are unique and all the values stored in a particular column are of same domain.
This table also satisfies the 2nd Normal Form as their is no Partial Dependency.
And, there is no Transitive Dependency, hence the table also satisfies the 3rd
Normal Form.
But this table is not in Boyce-Codd Normal Form.
student_id p_id
101 1
101 2
and so on...
1 P.Java Java
2 P.Cpp C++
and so on...
And now, this relation satisfy Boyce-Codd Normal Form. In the next tutorial we will
learn about the Fourth Normal Form.
1 Science Cricket
1 Maths Hockey
2 C# Cricket
2 Php Hockey
As you can see in the table above, student with s_id 1 has opted for two
courses, Science and Maths, and has two hobbies, Cricket and Hockey.
You must be thinking what problem this can lead to, right?
Well the two records for student with s_id 1, will give rise to two more records, as
shown below, because for one student, two hobbies exists, hence along with both the
courses, these hobbies should be specified.
1 Science Cricket
1 Maths Hockey
1 Science Hockey
1 Maths Cricket
s_id course
1 Science
1 Maths
2 C#
2 Php
s_id hobby
1 Cricket
1 Hockey
2 Cricket
2 Hockey
1 NF 2 NF 3 NF BCNF