Sei sulla pagina 1di 5

WORKSHOP SQL

Contents
Data Definition Language........................................................................................... 1
Constraints................................................................................................................. 2
Data Manipulation Language...................................................................................... 3
Retrieving Data from database................................................................................... 3
Views.......................................................................................................................... 4
Single row functions and group functions...................................................................4

Data Definition Language


1. Create a table JOBS with the following columns and data types:
JOB_IDNUMBERPRIMARYKEY,
JOB_TITLEVARCHAR2(35BYTE)NOTNULL,
MIN_SALARYNUMBER(6),
MAX_SALARYNUMBER(6)

2. Create a table DEPARTMENTS with the following columns and data types:
DEPARTMENT_IDNUMBERPRIMARYKEY,
DEPARTMENT_NAMEVARCHAR2(30BYTE)NOTNULL,
LOCATION_IDNUMBER

3. Create a table EMPLOYEES with the following columns and data types:
EMPLOYEE_IDNUMBERPRIMARYKEY,
FIRST_NAMEVARCHAR2(20BYTE),
LAST_NAMEVARCHAR2(25BYTE)NOTNULL,
EMAILVARCHAR2(25BYTE)NOTNULL,
PHONE_NUMBERVARCHAR2(20BYTE),
HIRE_DATEDATENOTNULL,
JOB_IDVARCHAR2(10BYTE)NOTNULL,
SALARYNUMBER(8,2),
COMMISSION_PCTNUMBER(2,2),
MANAGER_IDNUMBER,
DEPARTMENT_IDNUMBER

4. [OPTIONAL-HOMEWORK] Create a table LOCATIONS with the following


columns and data types:
LOCATION_IDNUMBERPRIMARYKEY,
STREET_ADDRESSVARCHAR2(40BYTE),
POSTAL_CODEVARCHAR2(12BYTE),
CITYVARCHAR2(30BYTE)NOTNULL,
STATE_PROVINCEVARCHAR2(25BYTE)

5. Create 3 sequences like below:


CREATESEQUENCETAB_DEPARTMENTS_SEQ
STARTWITH1
INCREMENTBY1;
CREATESEQUENCETAB_EMPLOYEES_SEQ
STARTWITH1
INCREMENTBY1;

[OPTIONAL-HOMEWORK] Create a sequence for table LOCATIONS

Constraints

1. Create the relations between the tables by alter the tables and add the FOREIGN
KEYS:
FK_EMPLOYEES_DEPARTMENTS
FK_EMPLOYEES_JOBS
FK_EMPLOYEES_EMPL_MANAGER
[OPTIONAL-HOMEWORK] FK_DEPARTMENTS_LOCATIONS
SYNTAX:
ALTER TABLE <TABLE1> ADD FOREIGN KEY (<FIELD_TABLE1>)
REFERENCES <TABLE2> (<FIELD_TABLE2>)

Data Manipulation Language


2. Insert exercises:
Insert a new record in the table DEPARTMENTS:
insertintodepartmentsvalues
(TAB_DEPARTMENTS_SEQ.nextval,
'Administration',
1700
);
insertintojobsvalues
('AD_PRES1'
,'President'
,20000
,40000
);
INSERTINTOemployees

VALUES(TAB_EMPLOYEES_SEQ.nextval,

'Steven',
'King',
'SKING',
'515.123.4567',
sysdate,
'AD_PRES',
24000,
NULL,
NULL,
90);

[OPTIONAL-HOMEWORK] Insert values into LOCATIONS table;

3. Run a ROLLBACK command.


4. Insert exercises Copy and Paste the content from file: import_values.sql and to
an sql editor and run them all.
5. Run a COMMIT command.

Retrieving Data from database


1. Play a little bit with the data SELECT clauses
a. Return all employees
b. Return all departments
c. Return all jobs
d. Return First Name and Last Name for all employees
2. Return all Employees from department 50
3. Increase salary by 30% for all employees in department 50 (UPDATE Statement)
4. Remove the employee with EMPLOYEE_ID 101 (DELETE Statement)
5. Return all Employees with job IT_PROG, ordered by their First Name.
6. Same as 2, but also return the department name in the SELECT clause
7. Using an Alias
selectcount(employee_id)fromemployeesempwhereemp.JOB_ID=
'IT_PROG'

Views
8. Creating a View with employees, department_name
CREATEVIEWEMPLOYEE_LISTAS
SELECTEMP.EMPLOYEE_ID,EMP.FIRST_NAME,DEPT.DEPARTMENT_NAME
FROMemployeesemp,departmentsdept
WHEREEMP.DEPARTMENT_ID=DEPT.DEPARTMENT_ID

Single row functions and group functions


1. Run the following SQL: SELECT sysdate from dual;
2. Format sysdate with TO_CHAR function.
selectto_char(sysdate,'ddMMyyyy')fromdual

3. Return a date from a varchar2 field


selectto_date('25112014','ddMMyyyy')fromdual

4. Return all employees First Name, with Upper case and Email with Lower case
5. Return all employees First Name with the prefix First Name: (use concat
function). After this, do the same using || operator

6.
7.
8.
9.

Use the Count function: return the total number of employees.


Return the number of employees with job IT_PROG
Return the number of employees for each department (count + group by)
Return the total salary for all employees in department 50 (use the SUM function)

Liquibase generation DDLs


1. Get jdbc driver for Oracle from master branch in Java Repository
2. Open a command prompt, go in the directory where the ojdbc6.jar stands and
run the following command:
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6
-Dversion=11.2.0.3 -Dpackaging=jar -Dfile=ojdbc6.jar -DgeneratePom=true
3. Add the oracle driver dependency inside the pom.xml:
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
4. Add liquibase plugin in pom.xml:
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.0.5</version>
<configuration>
<changeLogFile>src/main/resources/liquibase_file.xml</changeLogFile>
<driver>oracle.jdbc.driver.OracleDriver</driver>
<url>jdbc:oracle:thin:@10.6.33.102:1521:orcl</url>
<username>username</username>
<password>password</password>
</configuration>
<executions>
<execution>
<phase>process-resources</phase>
<goals>
<goal>update</goal>
</goals>
</execution>
</executions>
</plugin>
5. Modify username and password tag from in the above plugin configuration
6. Add the file liquibase_file.xml in src/main/resources
7. Run the compile task in Maven

Potrebbero piacerti anche