Syntax cheatsheet for the DB1 (databases) course at HdM Stuttgart
2022-02-01
“Come, let us go down and confuse their language so they will not understand each other” - Genesis 11:7, Die Bibel
Mehr Details unter https://github.com/pojntfx/uni-db1-notes. Dieses Dokument ist nur als Schnell-Übersicht gedacht.
create table persons (
number primary key not null ,
person_id varchar2(50),
first_name varchar2(50) default 'Duck' not null
last_name );
drop table persons;
alter table persons rename to people;
alter table persons add ( phone varchar2(20), email varchar2(100) )
alter table persons modify ( birthdate date null, email varchar2(255) );
alter table persons drop column birthdate;
alter table purchase_orders add constraint purchase_orders_order_id_pk primary key(order_id);
alter table purchase_orders drop constraint purchase_orders_order_id_pk;
create view employees_years_of_service
as select
|| ' ' || last_name as full_name,
employee_id, first_name floor(months_between(current_date, hire_date) / 12) as years_of_service
from employees;
drop view employees_years_of_service;
create index members_full_name on members(first_name, last_name);
drop index members_full_name;
create trigger customers_credit_trigger
before update of credit_limit
on customers
declare
number;
current_day begin
:= extract(day from sysdate);
current_day
if current_day between 28 and 31 then
-20100, 'Locked at the end of the month');
raise_application_error(end if;
end;
drop trigger customers_credit_trigger;
create trigger users_ensure_trigger
before update
on users
for each row
declare
exception;
user_invalid -20555);
pragma exception_init(user_invalid, begin
raise user_invalid;
exception
when user_invalid then
-20555, 'User is invalid');
raise_application_error(when others then
'Unexpected error: ' || sqlerrm);
dbms_output.put_line(end;
create or replace function get_my_sum( a integer, b integer ) return integer
is
number := 2;
multiplier begin
return a + b * multiplier;
end;
select get_my_sum(1, 2) from dual;
drop function get_my_sum;
create or replace procedure get_sum ( a integer, b integer )
is
number := 2;
multiplier number := 0;
result begin
:= a + b * multiplier;
result
insert into results ( result ) values ( result );
end;
exec get_sum(1, 2);
drop procedure get_sum;
CHAR|CHARACTER (size)
VARCHAR2 (size)
DATE
INTERVAL YEAR TO MONTH
INTERVAL DAY TO SECOND
INTEGER|INT
NUMBER (precision [, scale ])
FLOAT (precision)
insert into discounts(
discount_name,
amount,
start_date,
expired_datevalues (
) 'Summer Promotion',
9.5,
date '2017-05-01',
date '2017-08-31'
)
update products
set list_price = 420
where list_price < 69;
delete from products
where list_price > 69;
Gleiche Anzahl von Spalten, mehr Zeilen.
select
first_name,
last_name,
email,'contact' as role
from contacts
union select
first_name,
last_name,
email,'employee' as role
from employees order by role
Mehr Spalten & mehr Zeilen
select
id as id_a,
a.as color_a,
a.color id as id_b,
b.as color_b
b.color from palette_a a
inner join palette_b b using(color);
select
id as id_a,
a.as color_a,
a.color id as id_b,
b.as color_b
b.color from palette_a a
left outer join palette_b b using(color);
select
id as id_a,
a.as color_a,
a.color id as id_b, b.color as color_b
b.from palette_a a
right outer join palette_b b using(color);
select
id as id_a,
a.as color_a,
a.color id as id_b,
b.as color_b
b.color from palette_a a
full outer join palette_b b using(color);
:old
ist nicht vorhanden.
create or replace trigger customers_credit_trigger
before insert of credit_limit
on customers
declare
number;
current_day begin
:= extract(day from sysdate);
current_day
if current_day between 28 and 31 then
-20100, 'Locked at the end of the month');
raise_application_error(end if;
end;
create or replace trigger customers_credit_limit_trigger
before update of credit_limit
on customers
for each row
when (new.credit_limit > 0)
begin
if :new.credit_limit >= 2*:old.credit_limit then
-20101, 'The new credit cannot be more than double the old credit!');
raise_application_error(end if;
end;
:new
ist nicht vorhanden.
create or replace trigger customers_audit_trigger
after delete
on customers
for each row
declare
varchar2(10);
transaction_type begin
:= case
transaction_type when updating then 'update'
when deleting then 'delete'
end;
insert into audits(
table_name,
transaction_name,
by_user,
transaction_datevalues (
) 'customers',
transaction_type,user,
sysdate
);end;
create or replace trigger create_customer_trigger
instead of insert on customers_and_contacts
for each row
declare
number;
current_customer_id begin
insert into customers(
name,
address,
website,
credit_limitvalues (
) :new.name,
:new.address,
:new.website,
:new.credit_limit
returning customer_id into current_customer_id;
)
insert into contacts(
first_name,
last_name,
email,
phone,
customer_idvalues (
) :new.first_name,
:new.last_name,
:new.email,
:new.phone,
current_customer_id
);end;
menhir
Wenn einem der Syntax schon nicht kompliziert genug ist, dann darf
man vor das declare
-Statement eines Triggers auch
noch folgendes sinnloses Konstrukt packen und statt :new
:neu
schreiben:
referencing new as neu old as alt
Danach hat man auch fünf Zeilen. Und fünf Hirnzellen weniger.
Wo wir schon dabei sind: Ist der sonst universelle Negations-Operator
!=
zu einfach? Zu simpel und zu verständlich? Wie wäre es
mit <>
; macht das genau selbe, ist aber
komplizierter™!