· 7 years ago · Oct 25, 2018, 07:56 AM
1drop database if exists ti3_20180915;
2create database if not exists ti3_20180915;
3use ti3_20180915;
4create table pracownicy(
5id integer not null primary key,
6imie varchar(16) not null,
7nazwisko varchar(32) not null
8);
9
10alter table pracownicy add column login varchar(8);
11alter table pracownicy add column haslo varchar(8);
12alter table pracownicy add column pin char(4);
13
14create table wyroby(
15id integer not null primary key,
16nazwa varchar(32) not null
17);
18
19create table magazyny(
20id smallint not null primary key,
21nazwa varchar(16) not null
22);
23
24create table lokalizacjaWyrobow(
25id integer not null primary key,
26wyrobid integer not null,
27magazynid smallint not null,
28ktododalid integer not null
29);
30
31
32alter table lokalizacjawyrobow add constraint
33foreign key fk_wyrobId(wyrobId)
34references wyroby(id)
35on update restrict on delete restrict;
36
37alter table lokalizacjawyrobow add constraint
38foreign key fk_magazynId(magazynId)
39references magazyny(id)
40on update restrict on delete restrict;
41
42alter table lokalizacjawyrobow add constraint
43foreign key fk_ktoDodalId(ktoDodalId)
44references pracownicy(id)
45on update restrict on delete restrict;
46
47
48
49insert into wyroby values (1, 'podkladka');
50insert into wyroby (id, nazwa) values (2, 'nakretka');
51insert into wyroby (nazwa, ID) values ('nakretka', 3);
52
53delete from wyroby where id = 3;
54
55insert into magazyny values (1, 'magazyn A');
56
57
58insert into pracownicy values
59(1, 'Jan', 'Kowalski', null, null, null);
60
61insert into lokalizacjaWyrobow
62(id, wyrobid, magazynid, ktododalid) values
63(1, 1 , 1 , 1);
64
65select lw.id,
66lw.wyrobId as kodWyrobu, w.nazwa as produkt,
67lw.magazynId as 'Kod Magazynu', m.nazwa as magazyn,
68lw.ktoDodalId, p.nazwisko, p.imie
69from lokalizacjaWyrobow as lw
70inner join magazyny as m on m.id = lw.magazynid
71inner join wyroby as w on w.id = lw.wyrobid
72left outer join pracownicy as p on p.id = lw.ktoDodalId
73;
74
75update magazyny set nazwa = 'magazyn 1' where id = 1;
76
77update magazyny set nazwa = 'magazyn 1.a' where id = (select m.id from magazyny as m where m. nazwa = 'magazyn1');