Sei sulla pagina 1di 4

Universidade de Santiago

Cidade de Assomada,

Rua 5 de Julho, CP 4

Ilha de Santiago – Cabo Verde

TBD – Tecnologias de Base de Dados

Pretende-se com este exercícios familiarizar os alunos com a sintaxe de queries para manipulação de
dados, utilizando o oracle.

Criação de Tabelas

Sintaxe:

CREATE [GLOBAL TEMPORARY] TABLE table_name (


column_name type [CONSTRAINT constraint_def DEFAULT default_exp]
[, column_name type [CONSTRAINT constraint_def DEFAULT default_exp]...]
)
[ON COMMIT {DELETE | PRESERVE} ROWS]
TABLESPACE table_space;

Onde

• GLOBAL TEMPORARY specifies that the table's rows are temporary and such tables are
known as temporary tables.
• The duration of the contents are specified by the ON COMMIT clause.
• A temporary table is visible to all sessions, but rows are specific to a session.
• Type specifies the type of a column.
• Constraint_def specifies the definition of a constraint on a column.
• default_exp specifies the expression used to assign a default value to a column.
• ON COMMIT controls the duration of the rows in a temporary table.
• DELETE specifies the rows are deleted at the end of a transaction.
• PRESERVE specifies the rows are deleted at the end of a session.
• If you commit ON COMMIT for a temporary table, the default is DELETE.

1
1) Exemplo - Create Table

SQL> -- create demo table


SQL> create table Employee(
2 ID VARCHAR2(4 BYTE) NOT NULL primary key,
3 First_Name VARCHAR2(10 BYTE),
4 Last_Name VARCHAR2(10 BYTE),
5 Start_Date DATE,
6 End_Date DATE,
7 Salary Number(8,2),
8 City VARCHAR2(10 BYTE),
9 Description VARCHAR2(15 BYTE)
10 )
11 /

2) Inserir Dados
SQL>
SQL> -- prepare data
SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date,
End_Date, Salary, City, Description)
2 values ('01','Jason', 'Martin', to_date('19960725','YYYYMMD
D'), to_date('20060725','YYYYMMDD'), 1234.56, 'Toronto', 'Programmer')
3 /

1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date,


End_Date, Salary, City, Description)
2 values('02','Alison', 'Mathews', to_date('19760321','YYYYMMDD'), to_dat
e('19860221','YYYYMMDD'), 6661.78, 'Vancouver','Tester')
3 /

1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date,


End_Date,Salary, City, Description)
2 values('03','James', 'Smith', to_date('19781212','YYYYMMD
D'), to_date('19900315','YYYYMMDD'), 6544.78, 'Vancouver','Tester')
3 /

1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date,


End_Date, alary, City, Description)
2 values('04','Celia', 'Rice', to_date('19821024','YYYYMMDD'), to_date('
19990421','YYYYMMDD'), 2344.78, 'Vancouver','Manager')
3 /

1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date,


End_Date, Salary, City,Description)
2 values('05','Robert', 'Black', to_date('19840115','YYYYMMD
D'), to_date('19980808','YYYYMMDD'), 2334.78, 'Vancouver','Tester')
3 /

2
1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date,


End_Date,Salary, City,Description)
2 values('06','Linda', 'Green', to_date('19870730','YYYYMMD
D'), to_date('19960104','YYYMMDD'), 4322.78,'New York', 'Tester')
3 /

1 row created.

SQL> insert into Employee(ID, First_Name, Last_Name, Start_Date,


End_Date,Salary, City,Description)
2 values('07','David', 'Larry', to_date('19901231','YYYYMMD
D'), to_date('19980212','YYYYMMDD'), 7897.78,'New York', 'Manager')
3 /

2) Pesquisas
Seleccionar todos os empregados:

SQL> select * from Employee

• Seleccionar todos os empregados com residência na cidade de Vancouver?


• Seleccionar todos os empregados com residência na cidade de Vancouver e com salário maior
que 2344,76 USD.

SQL> SELECT city, AVG(salary)


2 FROM employee
3 GROUP BY city
4 HAVING AVG(salary) > 50000;

3) Copiar tabelas
Seleccionar todos os empregados com residência na cidade de Vancouver e com salário maior que
2344,76 USD.

SQL>
SQL> create table emp_copy as

3
select * from employee;

Table created.

SQL>
SQL> select * from emp_copy;

1) ALTERAR Tabelas
SQL>
SQL> ALTER TABLE employee
ADD modified_by INTEGER;

1) Apagar Tabelas
SQL> drop table emp_copy;

Table dropped.

Potrebbero piacerti anche