· 7 years ago · Nov 09, 2018, 07:30 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
48insert into wyroby values(1, 'podkladka');
49insert into wyroby (id, nazwa) values (2, 'nakretka');
50insert into wyroby ( nazwa, id) values ('nakrętka', 3);
51
52delete from wyroby where id = 3;
53
54insert into pracownicy values
55(1, 'jan','kowalski',null,null,null);
56
57insert into magazyny values
58(1, 'magazyn A');
59
60insert into lokalizacjaWyrobow
61(id, wyrobyID, magazynyID, ktoDodalID) values
62(1 , 1 , 1 , 1);
63
64insert into wyroby values
65(3, 'Profil plaski 20x30x1'),
66(4, 'Rura D=8mm/d6=mm');
67
68select lw.ID,
69lw.magazynyID as 'Kod Magazynu', m.nazwa as magazyn,
70lw.wyrobyID as kodWyrobu, w.nazwa as produkt,
71lw.ktoDodalID, p.nazwisko, p.imie
72from lokalizacjaWyrobow as lw
73inner join magazyny as m on m.ID = lw.magazynyID
74inner join wyroby as w on w.ID = lw.wyrobyID
75inner join pracownicy as p on p.ID = lw.ktoDodalID;
76
77update magazyny set nazwa = 'magazyn1'
78where id = 1;
79
80delete from wyroby where id in(3,4);
81
82select * from wyroby;
83
84delete from wyroby where id not in(3,4);
85
86update wyroby set nazwa = concat(nazwa, 'ver.1.0') where id in (1, 2, 3);
87
88select w.* from wyroby as w;
89select w.* from wyroby as w order by w.nazwa asc;
90select w.* from wyroby as w order by w.nazwa desc;
91select w.* from wyroby as w order by w.nazwa ;
92
93
94
95insert into pracownicy values (2, 'Kowalski', 'Adam', null, null, null);
96insert into pracownicy values (3, 'Malinowski', 'Adam', null, null, null);
97
98select * from pracownicy;
99select * from pracownicy as p order by p.nazwisko asc, p.imie desc;