· 7 years ago · Nov 28, 2018, 01:04 PM
1drop database if exists ti3_20181019;
2create database if not exists ti3_20181019;
3use ti3_20181019;
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
14
15create table wyroby(
16id integer not null primary key,
17nazwa varchar(32) not null
18);
19
20create table magazyny(
21id smallint not null primary key,
22nazwa varchar(16)
23);
24
25create table lokalizacjaWyrobow(
26id integer not null primary key,
27wyrobId integer not null,
28magazynId smallint not null,
29ktoDodalId integer not null
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
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
53
54
55
56insert into magazyny values (1, 'magazyn A');
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
65insert into wyroby values
66(3, 'Profil plaski 20x30x1'),
67(4, 'Rura D=8mm/d=6mm');
68
69
70
71
72select lw.id,
73lw.wyrobId as kodWyrobu, w.nazwa as produkt,
74lw.magazynId as 'Kod Magazynu', m.nazwa as magazyn,
75lw.ktoDodalId , p.nazwisko, p.imie
76from lokalizacjaWyrobow as lw
77inner join magazyny as m on m.id = lw.magazynId
78inner join wyroby as w on w.id = lw.wyrobId
79inner join pracownicy as p on p.id = lw.ktoDodalId
80;
81
82
83
84update magazyny set nazwa = 'magazyn 1'
85 where id = 1;
86
87
88
89delete from wyroby where id in (3, 4);
90
91select * from wyroby;
92
93-- delete from wyroby where id not in (3, 4);
94
95update wyroby set nazwa = concat(nazwa, ' ver.1.0') where id in (1, 2, 3);
96
97select w.* from wyroby as w;
98select w.* from wyroby as w order by w.nazwa asc;
99select w.* from wyroby as w order by w.nazwa desc;
100select w.* from wyroby as w order by w.nazwa;
101
102
103insert into pracownicy values (2, 'Adam', 'Kowalski', null, null, null);
104insert into pracownicy values (3, 'Adam', 'Malinowski', null, null, null);
105
106select * from pracownicy;
107select * from pracownicy as p order by p.nazwisko asc, p.imie desc;
108
109select p.id, p.imie, upper(p.nazwisko) as nazwisko
110 from pracownicy as p order by p.nazwisko asc;
111
112
113
114select p.* from pracownicy as p where p.imie = 'adam';