source: scripts/kreiranje.sql

Last change on this file was cc5ce51, checked in by todstef <todorovskistefan2@…>, 19 months ago

Commit scripts

  • Property mode set to 100644
File size: 6.1 KB
Line 
1--delete tables
2drop table if exists project.UserFavourites;
3drop table if exists project.UserComments;
4drop table if exists project.Ratings;
5drop table if exists project.WatchListProducts;
6drop table if exists project.ProductImages;
7drop table if exists project.ProductInstances;
8drop table if exists project.belongs_to;
9drop table if exists project.Users;
10drop table if exists project.Roles;
11drop table if exists project.Products;
12drop table if exists project.ProductCharacteristics;
13drop table if exists project.Manufacturers;
14drop table if exists project.Categories;
15drop table if exists project.Stores;
16
17
18--create tables
19create table project.Roles(
20 role_id serial not null,
21 role_description varchar(300) not null,
22 role_name varchar(100) not null,
23
24 constraint pk_Roles primary key (role_id),
25 constraint un_Roles_roleName unique (role_name)
26);
27
28create table project.Users(
29 user_id serial not null,
30 user_username varchar(100) not null,
31 user_password varchar(200) not null,
32 user_name varchar(100) not null,
33 user_surname varchar(100) not null,
34 user_picture_url varchar(200),
35 user_email varchar(100) not null,
36 user_phone_number varchar(100) not null,
37
38 role_id integer not null,
39
40 constraint pk_Users primary key (user_id),
41 constraint un_Users_userUsername unique (user_username),
42 constraint un_Users_userEmail unique (user_email),
43 constraint fk_Users_Roles foreign key (role_id) references project.Roles(role_id)
44);
45
46
47create table project.Manufacturers(
48 manufacturer_id serial not null,
49 manufacturer_name varchar(100) not null,
50 manufacturer_country varchar(100) not null,
51
52 constraint pk_Manufacturers primary key (manufacturer_id),
53 constraint un_Manufacturers_manufacturerName unique (manufacturer_name)
54);
55
56create table project.Categories(
57 category_id serial not null,
58 parent_category_id integer,
59 category_name varchar(100) not null,
60
61 constraint pk_Categories primary key (category_id),
62 constraint un_Categories_categoryName unique (category_name),
63 constraint fk_Categories_Categories foreign key (parent_category_id) references project.Categories(category_id)
64);
65
66create table project.ProductCharacteristics(
67 characteristic_id serial not null,
68 characteristic_description varchar(300) not null,
69
70 constraint pk_ProductCharacteristics primary key (characteristic_id)
71);
72
73create table project.Products (
74 product_id serial not null,
75 product_name varchar(100) not null,
76 post_date date not null,
77
78 manufacturer_id integer not null,
79 category_id integer not null,
80 characteristic_id integer not null,
81
82 constraint pk_Products primary key (product_id),
83 constraint un_Products_productName unique (product_name),
84 constraint un_Products_characteristicId unique (characteristic_id),
85 constraint fk_Products_Manufacturers foreign key (manufacturer_id) references project.Manufacturers(manufacturer_id),
86 constraint fk_Products_Categories foreign key (category_id) references project.Categories(category_id),
87 constraint fk_Products_ProductCharacteristics foreign key (characteristic_id) references project.ProductCharacteristics(characteristic_id)
88);
89
90create table project.UserFavourites(
91 user_favourite_id serial not null,
92 user_id integer not null,
93 product_id integer not null,
94
95 constraint pk_UserFavourites primary key (user_favourite_id),
96 constraint fk_UserFavourites_Users foreign key (user_id) references project.Users(user_id),
97 constraint fk_UserFavourites_Products foreign key (product_id) references project.Products(product_id)
98);
99
100create table project.UserComments(
101 comment_id serial not null,
102 user_comment varchar(300) not null,
103
104 user_id integer not null,
105 product_id integer not null,
106
107 constraint pk_UserComments primary key (comment_id),
108 constraint fk_UserComments_Users foreign key (user_id) references project.Users(user_id),
109 constraint fk_UserComments_Products foreign key (product_id) references project.Products(product_id)
110);
111
112create table project.Ratings (
113 rating_id serial not null,
114 rating_value integer not null,
115
116 user_id integer not null,
117 product_id integer not null,
118
119 constraint pk_Ratings primary key (rating_id),
120 constraint fk_Ratings_Users foreign key (user_id) references project.Users(user_id),
121 constraint fk_Ratings_Products foreign key (product_id) references project.Products(product_id)
122);
123
124create table project.WatchListProducts(
125 watch_list_product_id serial not null,
126 price_limit integer not null,
127
128 user_id integer not null,
129 product_id integer not null,
130
131 constraint pk_WatchListProducts primary key (watch_list_product_id),
132 constraint fk_WatchListProducts_Users foreign key (user_id) references project.Users(user_id),
133 constraint fk_WatchListProducts_Products foreign key (product_id) references project.Products(product_id)
134);
135
136create table project.ProductImages(
137 product_image_id serial not null,
138 product_image_url varchar(100) not null,
139
140 product_id integer not null,
141
142 constraint pk_ProductImages primary key (product_image_id),
143 constraint fk_ProductImages_Products foreign key (product_id) references project.Products(product_id)
144);
145
146create table project.Stores(
147 store_id serial not null,
148 store_name varchar(100) not null,
149
150 constraint pk_Stores primary key (store_id),
151 constraint un_Stores_storeName unique (store_name)
152);
153
154create table project.ProductInstances(
155 product_instance_id serial not null,
156 product_instance_price integer not null,
157
158 product_id integer not null,
159 store_id integer not null,
160
161 constraint pk_ProductInstances primary key (product_instance_id),
162 constraint fk_ProductInstances_Products foreign key (product_id) references project.Products(product_id),
163 constraint fk_ProductInstances_Stores foreign key (store_id) references project.Stores(store_id)
164);
165
166
167create table project.belongs_to(
168 product_id integer not null,
169 category_id integer not null,
170
171 constraint pk_belongs_to primary key (product_id, category_id),
172 constraint fk_belongs_to_Products foreign key (product_id) references project.Products(product_id),
173 constraint fk_belongs_to_Categories foreign key (category_id) references project.Categories(category_id)
174);
175
Note: See TracBrowser for help on using the repository browser.