0% found this document useful (0 votes)
28 views

Dbslab 04

This document discusses using the MERGE statement in SQL to perform an UPSERT operation, which inserts new rows into a table if they don't exist or updates existing rows if they do exist. It provides an example using two tables, t1 and t2, where data from t1 is merged into t2. If a match is found, the rows in t2 are updated, and if not, new rows are inserted.

Uploaded by

Huzaifa Ch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Dbslab 04

This document discusses using the MERGE statement in SQL to perform an UPSERT operation, which inserts new rows into a table if they don't exist or updates existing rows if they do exist. It provides an example using two tables, t1 and t2, where data from t1 is merged into t2. If a match is found, the rows in t2 are updated, and if not, new rows are inserted.

Uploaded by

Huzaifa Ch
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Introduction to Database Systems

Experiment 4
Implementation of tasks on DML (Data Manipulation Language). SQL Aliases,
implementation of upsert and Merge queries.

CLO-3: Use modern tools and languages.


CLO-4: Build the projects of varying complexities.
CLO-5: Demonstrate the implementation of problem solving process.
CLO-6: Work individually as well as in teams.
CLO-7: Propose a unique solution that exhibits originality of idea

Engr. Rabia Arshad


2. ALTER:

(a)ALTER TABLE ...ADD...: This is used to add some extra fields into existing relation.

Syntax: ALTER TABLE relation_name ADD(new field_1 data_type(size), new field_2


data_type(size),..);

Example : SQL>ALTER TABLE std ADD(Address CHAR(10));

(b)ALTER TABLE...MODIFY...: This is used to change the width as well as data type of
fields of existing relations.

Syntax: ALTER TABLE relation_name MODIFY (field_1 newdata_type(Size), field_2


newdata_type(Size),....field_newdata_type(Size));

Example:SQL>ALTER TABLE student MODIFY(sname VARCHAR(10),class VARCHAR(5));

3. DROP

A) DROP TABLE: This is used to delete the structure of a relation. It permanently deletes
the records in the table.

Syntax: DROP TABLE relation_name;

Example: SQL>DROP TABLE std;

B) DROP COLUMN: This is used to delete specific column in a relation.

Syntax: ALTER TABLE relation_name DROP COLUMN column_name;

Example: SQL>ALTER TABLE student DROP COLUMN sname;

4. RENAME: It is used to modify the name of the existing database object.


A) RENAME COLUMN IN TABLE
To rename a column in an existing table, the SQL ALTER TABLE syntax is:

Syntax:
ALTER TABLE TABLE_NAME
RENAME COLUMN OLD_NAME TO NEW_NAME;

Example: SQL>ALTER TABLE student RENAME COLUMN sname TO firstName;

B) RENAME TABLE
To rename a table, the SQL ALTER TABLE syntax is:

Syntax:
ALTER TABLE TABLE_NAME RENAME TO NEW_TABLE_NAME;

Example SQL>ALTER TABLE student RENAME to std6;

5. TRUNCATE: This command will remove the data permanently. But structure will not be
removed.

Syntax: TRUNCATE TABLE <Table name>

Example TRUNCATE TABLE student;

Difference between Truncate & Delete:-

 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 2. UPDATE 3. DELETE

1. INSERT INTO: This is used to add records into a relation. These are three type of
INSERT INTO queries which are as

a) Inserting a single record

Syntax: INSERT INTO relationname(field_1,field_2,.field_n)VALUES

(data_1,data_2,........data_n);

Example: SQL>INSERT INTO student(sno,sname,class,address)VALUES

(1,’Raheel’,’Tenth’,’RWP’);

b) Inserting all records from another relation

Syntax: INSERT INTO relation_name_1 SELECT Field_1,field_2,field_n

FROM relation_name_2 WHERE field_x=data;

Example: SQL>INSERT INTO std SELECT sno,sname FROM student

WHERE name = ‘Rohail‘;

c) Inserting multiple records

Syntax: INSERT INTO relation_name field_1,field_2,.....field_n) VALUES

(&data_1,&data_2,........&data_n);

Example: SQL>INSERT INTO student(sno,sname,class,address)

VALUES(&sno,’&sname’,’&class’,’&address’);

Enter value for sno: 101


Enter value for name: Rehan

Enter value for class: Seventh

Enter value for name: Ahmad

2. UPDATE-SET-WHERE: This is used to update the content of a record in a relation.

Syntax: SQL>UPDATE relation name SET Field_name1=data,field_name2=data,

WHERE field_name=data;

Example: SQL>UPDATE student SET sname = ‘Ali’ WHERE sno=1;

3. DELETE-FROM: This is used to delete all the records of a relation but it will retain the
structure of that relation.

a) DELETE-FROM: This is used to delete all the records of relation.

Syntax: SQL>DELETE FROM relation_name;

Example: SQL>DELETE FROM std;

b) DELETE -FROM-WHERE: This is used to delete a selected record from a relation.

Syntax: SQL>DELETE FROM relation_name WHERE condition;

Example: SQL>DELETE FROM student WHERE sno = 2;


TASKS:

1) Create all the tables given above with columns specified:


Coulmn_ID should be Int type, Name is VarChar

2) Based on the departments table below, rename the departments table to depts.

CREATE TABLE departments

( department_id number(10) not null,

department_name varchar2(50) not null);


3) Based on the employees table below, add a column called salary that is a number(6) datatype.

CREATE TABLE employees( employee_number number(10) not null,

employee_name varchar2(50) not null,

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.

CREATE TABLE customers

( customer_id number(10) not null,

customer_name varchar2(50) not null,

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:

CREATE TABLE employees

( employee_number number(10) not null,

employee_name >varchar2(50) not null,

department_id number(10)

6) Based on the employees table below, drop the salary column.

CREATE TABLE employees

( employee_number number(10) not null,


employee_name varchar2(50) not null,

department_id number(10),

salary number(6),

CONSTRAINT employees_pk PRIMARY KEY (employee_number));

9) Based on the departments table below, rename the department_name column to dept_name.

CREATE TABLE departments

( department_id number(10) not null,

department_name varchar2(50) not null,

CONSTRAINT departments_pk PRIMARY KEY (department_id) );

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.

EXAMPLE - ALIAS A COLUMN


 Generally, aliases are used to make the column headings in your result set easier to
read. For example, when concatenating fields together, you might alias the result.

For example:

SELECT contact_id, first_name || last_name AS NAME


FROM contacts
WHERE last_name = 'Anderson';

Oracle: CONCAT( ), || Sometimes it is necessary to combine together (concatenate) the results


from several different fields.
In this example, we've aliased the second column (ie: first_name and last_name concatenated)
as NAME. As a result, NAME will display as the heading for the second column when the result
set is returned. Because our alias_name did not include any spaces, we are not required to
enclose the alias_name in quotes.
However, it would have been perfectly acceptable to write this example using quotes as
follows:

SELECT contact_id, first_name || last_name AS "NAME"


FROM contacts
WHERE last_name = 'Anderson';

Next, let's look at an example where we are required to enclose the alias_name in quotes.

For example:

SELECT contact_id, first_name || last_name AS "CONTACT NAME"


FROM contacts
WHERE last_name = 'Anderson';

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.

To perform the UPSERT operation we can use the MERGE statement.


EXAMPLE:

CREATE TABLE t1
(id VARCHAR2(5) ,
value VARCHAR2(5),
value2 VARCHAR2(5)
);

CREATE TABLE t2
(id VARCHAR2(5) ,
value VARCHAR2(5),
value2 VARCHAR2(5))

insert into t1 values ('a','1','1');


insert into t1 values ('b','4','5');
insert into t2 values ('b','2','2');
insert into t2 values ('c','3','3');

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

You might also like