· 7 years ago · Oct 25, 2018, 09:18 AM
1drop database if exists ABD_14092018;
2create database if not exists ABD_14092018;
3use ABD_14092018;
4
5create table pracownicy(
6 ID integer not null primary key,
7 imie varchar(16) not null,
8 nazwisko varchar(32) not null
9);
10
11alter table pracownicy add column login varchar(8);
12alter table pracownicy add column haslo varchar(8);
13alter table pracownicy add column pin char(4);
14
15create table wyroby(
16 ID integer not null primary key,
17 nazwa varchar(32) not null
18);
19
20create table magazyny(
21 ID smallint not null primary key,
22 nazwa varchar(16)
23);
24
25create table lokalizacjaWyrobow(
26 ID integer not null primary key,
27 wyrobyID integer not null,
28 magazynyID smallint not null,
29 ktoDodalID integer not null
30);
31
32alter table lokalizacjaWyrobow add constraint
33foreign key fk_wyrobyID(wyrobyID)
34references wyroby(id)
35on update restrict on delete restrict;
36
37alter table lokalizacjaWyrobow add constraint
38foreign key fk_magazynyID(magazynyID)
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 ('nakrętka', 3);
52
53delete from wyroby where id = 3;
54
55insert into pracownicy values
56(1, 'jan','kowalski',null,null,null);
57
58insert into magazyny values
59(1, 'magazyn A');
60
61insert into lokalizacjaWyrobow
62(id, wyrobyID, magazynyID, ktoDodalID) values
63(1 , 1 , 1 , 1);
64
65insert into wyroby values
66(3, 'Profil plaski 20x30x1'),
67(4, 'Rura D=8mm/d6=mm');
68
69select lw.ID,
70lw.magazynyID as 'Kod Magazynu', m.nazwa as magazyn,
71lw.wyrobyID as kodWyrobu, w.nazwa as produkt,
72lw.ktoDodalID, p.nazwisko, p.imie
73 from lokalizacjaWyrobow as lw
74inner join magazyny as m on m.ID = lw.magazynyID
75inner join wyroby as w on w.ID = lw.wyrobyID
76inner join pracownicy as p on p.ID = lw.ktoDodalID
77;
78
79update magazyny set nazwa = 'magazyn1'
80where id = 1;
81
82update magazyny set nazwa = 'magazyn 1.a'
83where id = (select id from magazyny where nazwa = 'magazyn 1');