Dbslab 04
Dbslab 04
Experiment 4
Implementation of tasks on DML (Data Manipulation Language). SQL Aliases,
implementation of upsert and Merge queries.
(a)ALTER TABLE ...ADD...: This is used to add some extra fields into existing relation.
(b)ALTER TABLE...MODIFY...: This is used to change the width as well as data type of
fields of existing relations.
3. DROP
A) DROP TABLE: This is used to delete the structure of a relation. It permanently deletes
the records in the table.
Syntax:
ALTER TABLE TABLE_NAME
RENAME COLUMN OLD_NAME TO NEW_NAME;
B) RENAME TABLE
To rename a table, the SQL ALTER TABLE syntax is:
Syntax:
ALTER TABLE TABLE_NAME RENAME TO NEW_TABLE_NAME;
5. TRUNCATE: This command will remove the data permanently. But structure will not be
removed.
By using truncate command data will be removed permanently & will not get back
where as by using delete command data will be removed temporally & get back
by using roll back command.
By using delete command data will be removed based on the condition where as
by using truncate command there is no condition.
Truncate is a DDL command & delete is a DML command.
2. DATA MANIPULATION LANGUAGE (DML): The Data Manipulation Language (DML) is
used to retrieve, insert and modify database information. These commands will be used
by all database users during the routine operation of the database. Let's take a brief look
at the basic DML commands:
1. INSERT INTO: This is used to add records into a relation. These are three type of
INSERT INTO queries which are as
(data_1,data_2,........data_n);
(1,’Raheel’,’Tenth’,’RWP’);
(&data_1,&data_2,........&data_n);
VALUES(&sno,’&sname’,’&class’,’&address’);
WHERE field_name=data;
3. DELETE-FROM: This is used to delete all the records of a relation but it will retain the
structure of that relation.
2) Based on the departments table below, rename the departments table to depts.
department_id number(10));
4) Based on the customers table below, add two columns - one column called contact_name that is a
varchar2(50) datatype and one column called last_contacted that is a date datatype.
address varchar2(50),
city varchar2(50),
state varchar2(25),
zip_code varchar2(10));
5) Create table using the following specifications and insert atleast 10 records using insert into
statements:
department_id number(10)
department_id number(10),
salary number(6),
9) Based on the departments table below, rename the department_name column to dept_name.
SQL ALIASES
DESCRIPTION
Oracle ALIASES can be used to create a temporary name for columns or tables.
COLUMN ALIASES are used to make column headings in your result set easier to
read.
TABLE ALIASES are used to shorten your SQL to make it easier to read or when
you are performing a self join (ie: listing the same table more than once in the FROM
clause).
NOTE
If the alias_name contains spaces, you must enclose the alias_name in quotes.
It is acceptable to use spaces when you are aliasing a column name. However, it is
not generally good practice to use spaces when you are aliasing a table name.
The alias_name is only valid within the scope of the SQL statement.
For example:
Next, let's look at an example where we are required to enclose the alias_name in quotes.
For example:
In this example, we've aliased the second column (ie: first_name and last_name concatenated)
as "CONTACT NAME". Since there are spaces in this alias_name, "CONTACT NAME" must be
enclosed in quotes.
Using the MERGE Statement to Perform an UPSERT
The term UPSERT has been coined to refer to an operation that inserts rows into a table if
they don’t exist, otherwise they are updated.
CREATE TABLE t1
(id VARCHAR2(5) ,
value VARCHAR2(5),
value2 VARCHAR2(5)
);
CREATE TABLE t2
(id VARCHAR2(5) ,
value VARCHAR2(5),
value2 VARCHAR2(5))
merge into t2
using t1
on (t1.id = t2.id)
when matched then
update set t2.value = t1.value,
t2.value2 = t1.value2
when not matched then
insert (t2.id, t2.value, t2.value2)
values(t1.id, t1.value, t1.value2);
Collation:
"A character set is a set of symbols and encodings. A collation is a set of rules for comparing
characters in a character set. Let's make the distinction clear with an example of an imaginary
character set.
Suppose that we have an alphabet with four letters: 'A', 'B', 'a', 'b'. We give each letter a number:
'A' = 0, 'B' = 1, 'a' = 2, 'c' = 3. The letter 'A' is a symbol, the number 0 is the encoding for 'A', and
the combination of all four letters and their encodings is a character set.
Now, suppose that we want to compare two string values, 'A' and 'B'. The simplest way to do this
is to look at the encodings: 0 for 'A' and 1 for 'B'. Because 0 is less than 1, we say 'A' is less than
'B'. Now, what we've just done is apply a collation to our character set. The collation is a set of
rules (only one rule in this case): "compare the encodings." We call this simplest of all possible
collations a binary collation.
But what if we want to say that the lowercase and uppercase letters are equivalent? Then we
would have at least two rules: (1) treat the lowercase letters 'a' and 'b' as equivalent to 'A' and 'B';
(2) then compare the encodings. We call this a case-insensitive collation. It's a little more
complex than a binary collation.
In real life, most character sets have many characters: not just 'A' and 'B' but whole alphabets,
sometimes multiple alphabets or an eastern writing system with thousands of characters, along
with many special symbols and punctuation marks. Also in real life, most collations have many
rules: not just case insensitivity but also accent insensitivity (an "accent" is a mark attached to a
character as in German 'ö') and multiple-character mappings (such as the rule that 'ö' = 'OE' in
one of the two German collations)."
Assignment #01:
TASK: (a) Insert following records by creating corresponding tables in your
database.
(b)Apply merge and update some records which exist. Also insert some new
record using merge.
CUSTOMER_T
ORDER_T
ORDER_LINE_T
PRODUCT_T