· 7 years ago · Oct 25, 2018, 07:58 AM
1drop database if exists ti_14092018;
2create database if not exists ti_14092018;
3use ti_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 alter table lokalizacjaWyrobow add constraint
32 foreign key fk_wyrobyId(wyrobyId)
33 references wyroby(id)
34 on update restrict on delete restrict;
35
36
37 alter table lokalizacjaWyrobow add constraint
38 foreign key fk_magazynyId(magazynyID)
39 references magazyny(id)
40 on update restrict on delete restrict;
41
42 alter table lokalizacjaWyrobow add constraint
43 foreign key fk_ktoDodalID(ktoDodalID)
44 references pracownicy(id)
45 on update restrict on delete restrict;
46
47insert into wyroby values(1, 'podkladka');
48insert into wyroby (id, nazwa) values (2, 'nakretka');
49insert into wyroby (nazwa, id) values ('nakretka', 3);
50
51delete from wyroby where id = 3;
52
53insert into magazyny values(1, 'magazyn A');
54
55insert into pracownicy values ( 1, 'Jan', 'Kowalski', null, null, null);
56
57insert into lokalizacjaWyrobow
58(id, wyrobyID, magazynyID, ktoDodalID) values
59(1, 1 , 1 , 1);
60
61select lw.id,
62lw.wyrobyID as kodWyrobu, w.nazwa as produkt,
63lw.magazynyID as 'Kod Magazynu', m.nazwa as magazyn,
64lw.ktoDodalID , p.nazwisko, p.imie
65from lokalizacjaWyrobow as lw
66inner join magazyny as m on m.id = lw.magazynyID
67inner join wyroby as w on w.id = lw.wyrobyID
68left outer join pracownicy as p on p.id = lw.ktoDodalID
69
70;
71
72update magazyny set nazwa = 'magazyn 1'
73 where id = 1;
74
75
76update magazyny set nazwa = 'magazyn 1.a'
77 where id = (select id from magazyny where nazwa = 'magazyn 1');