· 7 years ago · Nov 08, 2018, 09:00 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
57insert into pracownicy values
58(1, 'Jan', 'Kowalski', null, null, null);
59
60insert into lokalizacjaWyrobow
61(id, wyrobid, magazynid, ktododalid) values
62(1, 1 , 1 , 1);
63
64insert into wyroby values
65(3, 'Profil plaski 20x30x1'),
66(4, 'Rura d=8mm/d=6mm');
67
68select lw.id,
69lw.wyrobId as kodWyrobu, w.nazwa as produkt,
70lw.magazynId as 'Kod Magazynu', m.nazwa as magazyn,
71lw.ktoDodalId, p.nazwisko, p.imie
72from lokalizacjaWyrobow as lw
73inner join magazyny as m on m.id = lw.magazynid
74inner join wyroby as w on w.id = lw.wyrobid
75left outer join pracownicy as p on p.id = lw.ktoDodalId
76;
77
78delete from wyroby where id in(3,4);
79
80select * f rom wyroby;
81
82delete from wyroby where id not in(3,4);
83
84update wyroby set nazwa = concat(nazwa, ' ver.1.0') where id in (1, 2, 3);
85
86update magazyny set nazwa = 'magazyn 1' where id = 1;