Line 1... |
Line 1... |
1 |
/*
|
1 |
/*
|
2 |
mySQL Database for Computer Asset Management Program
|
2 |
mySQL Database for Computer Asset Management Program
|
3 |
No records are deleted by default. If record is to be deleted
|
3 |
No records are deleted by default. If record is to be deleted
|
4 |
it is marked as removed (removed_date not null)
|
4 |
it is marked as removed (removed_date not null)
|
5 |
When joining tables (ie client_asset) is modified, the old record is
|
5 |
When joining tables (ie client_device) is modified, the old record is
|
6 |
marked removed and a new record added, to give history of device
|
6 |
marked removed and a new record added, to give history of device
|
7 |
*/
|
7 |
*/
|
8 |
|
8 |
|
9 |
/* used as a configuration file for the app as a whole */
|
9 |
/* used as a configuration file for the app as a whole */
|
- |
|
10 |
drop table if exists _system;
|
10 |
create or replace table _system (
|
11 |
create table _system (
|
11 |
_system_id int unsigned not null auto_increment,
|
12 |
_system_id int unsigned not null auto_increment,
|
12 |
group_name varchar(64) NOT NULL COMMENT 'used to group keys together',
|
13 |
group_name varchar(64) NOT NULL COMMENT 'used to group keys together',
|
13 |
key_name varchar(64) NOT NULL COMMENT 'key into this value',
|
14 |
key_name varchar(64) NOT NULL COMMENT 'key into this value',
|
14 |
theValue text null COMMENT 'the actual value of this entry',
|
15 |
theValue text null COMMENT 'the actual value of this entry',
|
15 |
added_date datetime not null COMMENT 'date record was added',
|
16 |
added_date date not null COMMENT 'date record was added',
|
16 |
removed_date datetime default NULL COMMENT 'date record was closed',
|
17 |
removed_date date default NULL COMMENT 'date record was closed',
|
17 |
PRIMARY KEY (_system_id )
|
18 |
PRIMARY KEY (_system_id )
|
18 |
) COMMENT='Basically a configuration file equivilent to a windows INI ';
|
19 |
) COMMENT='Basically a configuration file equivilent to a windows INI ';
|
19 |
|
20 |
|
20 |
/* used by the auth class */
|
21 |
/* used by the auth class */
|
- |
|
22 |
drop table if exists _user;
|
21 |
create or replace table user (
|
23 |
create table _user (
|
22 |
user_id int unsigned not null auto_increment,
|
24 |
_user_id int unsigned not null auto_increment,
|
- |
|
25 |
username varchar(32) comment 'user name for logging in',
|
23 |
name varchar(64) comment 'common name of user',
|
26 |
name varchar(64) comment 'common name of user',
|
24 |
email varchar(64) comment 'email address of user',
|
27 |
email varchar(64) comment 'email address of user',
|
25 |
notes text comment 'any notes about user',
|
28 |
notes text comment 'any notes about user',
|
26 |
pass varchar(256) comment 'encrypted password of user',
|
29 |
pass varchar(256) comment 'encrypted password of user',
|
27 |
access text comment 'sql to determine what records user can view',
|
30 |
access text comment 'sql to determine what records user can view',
|
28 |
added_date date not null default now() comment 'Date record added to database',
|
31 |
added_date date not null comment 'Date record added to database',
|
29 |
removed_date date null default null comment 'Date record marked as removed',
|
32 |
removed_date date default null comment 'Date record marked as removed',
|
30 |
primary key (user_id)
|
33 |
primary key ( _user_id )
|
31 |
) comment 'user access to program';
|
34 |
) comment 'user access to program';
|
32 |
|
35 |
|
33 |
/* used by the menu class */
|
36 |
/* used by the menu class */
|
- |
|
37 |
drop table if exists _menu;
|
34 |
create or replace table menu (
|
38 |
create table _menu (
|
35 |
menu_id int unsigned not null auto_increment,
|
39 |
_menu_id int unsigned not null auto_increment,
|
36 |
parent_id int unsigned null comment 'If this is a submenu the id of the parent' REFERENCES menu(menu_id),
|
40 |
parent_id int unsigned null comment 'If this is a submenu the id of the parent' REFERENCES _menu(_menu_id),
|
37 |
caption varchar(20) not null comment 'The actual caption displayed',
|
41 |
caption varchar(20) not null comment 'The actual caption displayed',
|
38 |
url varchar(120) null comment 'the url of the page/script to call or null if this contains sub-options',
|
42 |
url varchar(120) null comment 'the url of the page/script to call or null if this contains sub-options',
|
39 |
primary key (menu_id)
|
43 |
primary key (_menu_id)
|
40 |
) comment 'We keep the entire menu structure here so modules can modify it';
|
44 |
) comment 'We keep the entire menu structure here so modules can modify it';
|
41 |
|
45 |
|
42 |
|
46 |
|
43 |
/* used by report class */
|
- |
|
44 |
create or replace table report (
|
- |
|
45 |
report_id int unsigned not null auto_increment,
|
- |
|
46 |
name varchar(64) not null comment 'Display Name of Report',
|
- |
|
47 |
query text not null comment 'Query to be executed',
|
- |
|
48 |
parameters text comment 'All parameters used in above',
|
- |
|
49 |
screen_report int unsigned default null comment 'Each screen can be assigned a bit and this will show up on a screen',
|
- |
|
50 |
primary key (report_id)
|
- |
|
51 |
) comment 'holds definition for report';
|
- |
|
52 |
|
- |
|
53 |
|
- |
|
54 |
/* beginning of the actual tables used by the app, client, site and asset */
|
47 |
/* beginning of the actual tables used by the app, client, site and device */
|
55 |
|
48 |
|
- |
|
49 |
/*
|
- |
|
50 |
A device is owned by a client (entity). This is a list of available
|
- |
|
51 |
clients who can own things.
|
- |
|
52 |
*/
|
- |
|
53 |
drop table if exists client;
|
56 |
create or replace table client (
|
54 |
create table client (
|
57 |
client_id int unsigned not null auto_increment,
|
55 |
client_id int unsigned not null auto_increment,
|
58 |
name varchar(64) comment 'Name of the client',
|
56 |
name varchar(64) comment 'Name of the client',
|
59 |
notes text comment 'Any notes you want to enter',
|
57 |
notes text comment 'Any notes you want to enter',
|
60 |
added_date date not null default now() comment 'Date record added to database',
|
58 |
added_date date not null comment 'Date record added to database',
|
61 |
removed_date date null default null comment 'Date record marked as removed',
|
59 |
removed_date date default null comment 'Date record marked as removed',
|
62 |
primary key (client_id)
|
60 |
primary key (client_id)
|
63 |
) comment 'Stores information about a particlar client/owner';
|
61 |
) comment 'Stores information about a particlar client/owner';
|
64 |
|
62 |
|
- |
|
63 |
/*
|
- |
|
64 |
A device can be on a site. This is a list of sites which can house
|
- |
|
65 |
devices
|
- |
|
66 |
*/
|
- |
|
67 |
drop table if exists site;
|
65 |
create or replace table site (
|
68 |
create table site (
|
66 |
site_id int unsigned not null auto_increment,
|
69 |
site_id int unsigned not null auto_increment,
|
67 |
name varchar(64) comment 'Name of the site',
|
70 |
name varchar(64) comment 'Name of the site',
|
68 |
notes text comment 'Any notes you want to enter',
|
71 |
notes text comment 'Any notes you want to enter',
|
69 |
added_date date not null default now() comment 'Date record added to database',
|
72 |
added_date date not null comment 'Date record added to database',
|
70 |
removed_date date null default null comment 'Date record marked as removed',
|
73 |
removed_date date default null comment 'Date record marked as removed',
|
71 |
primary key (site_id)
|
74 |
primary key (site_id)
|
72 |
) comment 'Stores information about a particlar physical site';
|
75 |
) comment 'Stores information about a particlar physical site';
|
73 |
|
76 |
|
74 |
|
77 |
/*
|
- |
|
78 |
The basis for the program. In our context, an asset is a computer or
|
- |
|
79 |
other device.
|
75 |
|
80 |
*/
|
- |
|
81 |
drop table if exists device;
|
76 |
create or replace table asset (
|
82 |
create table device (
|
77 |
asset_id int unsigned not null auto_increment,
|
83 |
device_id int unsigned not null auto_increment,
|
78 |
name varchar(64) comment 'name of the device or asset',
|
84 |
name varchar(64) comment 'name of the device or device',
|
79 |
notes text comment 'any notes we want to store',
|
85 |
notes text comment 'any notes we want to store',
|
80 |
part_of int unsigned null references asset( asset_id),
|
- |
|
81 |
asset_type_id int unsigned not null references asset_type( asset_type_id ),
|
86 |
device_type_id int unsigned not null references device_type( device_type_id ),
|
82 |
added_date date not null default now() comment 'Date record added to database',
|
87 |
added_date date not null comment 'Date record added to database',
|
83 |
removed_date date null default null comment 'Date record marked as removed',
|
88 |
removed_date date default null comment 'Date record marked as removed',
|
84 |
primary key (asset_id)
|
89 |
primary key (device_id)
|
85 |
) comment 'stores information about an individual device or other asset';
|
90 |
) comment 'stores information about an individual device or other device';
|
86 |
|
91 |
|
- |
|
92 |
/*
|
- |
|
93 |
allows devices to be related to each other, ie a "part of" scheme, where
|
- |
|
94 |
for example, a virtualized server may be 'part of' a physical machine.
|
- |
|
95 |
By using a child table, we can track movement across this relationship
|
- |
|
96 |
*/
|
- |
|
97 |
drop table if exists device_device;
|
87 |
create or replace table asset_relationship (
|
98 |
create table device_device (
|
88 |
asset_relationship_id int unsigned not null auto_increment,
|
99 |
device_device_id int unsigned not null auto_increment,
|
89 |
source int unsigned not null references asset( asset_id ),
|
100 |
source int unsigned not null references device( device_id ),
|
90 |
target int unsigned not null references asset( asset_id ),
|
101 |
target int unsigned not null references device( device_id ),
|
- |
|
102 |
added_date date not null comment 'Date record added to database',
|
- |
|
103 |
removed_date date default null comment 'Date record marked as removed',
|
91 |
primary key (asset_relationship_id)
|
104 |
primary key (device_device_id)
|
92 |
) comment 'joins asset to another asset';
|
105 |
) comment 'joins device to another device';
|
93 |
|
106 |
|
94 |
/*
|
107 |
/*
|
95 |
asset_type is a child table of asset, determining what type of asset it is]
|
108 |
device_type is a child table of device, determining what type of device it is]
|
96 |
such as computer, printer, router, whatever.
|
109 |
such as computer, printer, router, whatever.
|
97 |
flags currently used as 0 is non system and 1 is system, though this could
|
110 |
flags currently used as 0 is non system and 1 is system, though this could
|
98 |
be expanded in the future
|
111 |
be expanded in the future
|
99 |
*/
|
112 |
*/
|
100 |
|
- |
|
- |
|
113 |
drop table if exists device_type;
|
101 |
create or replace table asset_type (
|
114 |
create table device_type (
|
102 |
asset_type_id int(10) unsigned NOT NULL auto_increment,
|
115 |
device_type_id int(10) unsigned NOT NULL auto_increment,
|
103 |
name varchar(64) not null COMMENT 'the visible displayed name',
|
116 |
name varchar(64) not null COMMENT 'the visible displayed name',
|
104 |
flags int unsigned default 1 comment 'flags for this asset type',
|
117 |
flags int unsigned default 1 comment 'flags for this device type',
|
105 |
added_date date not null COMMENT 'date record was added',
|
118 |
added_date date not null COMMENT 'date record was added',
|
106 |
removed_date date default NULL COMMENT 'date record was deleted/supserceded',
|
119 |
removed_date date default NULL COMMENT 'date record was deleted/supserceded',
|
107 |
primary key (asset_type_id)
|
120 |
primary key (device_type_id)
|
108 |
) comment='simply a list of device types ie computer printer whatever';
|
121 |
) comment='simply a list of device types ie computer printer whatever';
|
109 |
|
122 |
|
110 |
|
123 |
|
111 |
/*
|
124 |
/*
|
112 |
this is actually a join with multiple tables, depending on what the
|
125 |
this is actually a join with multiple tables, depending on what the
|
113 |
name is associated with, client, site or asset
|
126 |
name is associated with, client, site or device
|
114 |
for example, if 'id' is client_id from the client table, then 'source'
|
127 |
for example, if 'id' is client_id from the client table, then 'source'
|
115 |
would be 'client' (name of the table);
|
128 |
would be 'client' (name of the table);
|
116 |
*/
|
129 |
*/
|
- |
|
130 |
drop table if exists alias;
|
117 |
create or replace table alias (
|
131 |
create table alias (
|
118 |
alias_id int unsigned not null auto_increment,
|
132 |
alias_id int unsigned not null auto_increment,
|
119 |
source varchar(64) comment 'the table this alias comes from',
|
133 |
source varchar(64) comment 'the table this alias comes from',
|
120 |
id int unsigned not null comment 'the client, site or asset id',
|
134 |
id int unsigned not null comment 'the client, site or device id',
|
121 |
name varchar(64) comment 'the alias for the asset',
|
135 |
name varchar(64) comment 'the alias for the device',
|
122 |
primary key (asset_alias_id)
|
136 |
primary key (alias_id)
|
123 |
) comment 'Allows client, site and asset to have multiple names';
|
137 |
) comment 'Allows client, site and device to have multiple names';
|
124 |
|
138 |
|
- |
|
139 |
/*
|
- |
|
140 |
links a site to a client. A site is "owned" by a client
|
- |
|
141 |
*/
|
- |
|
142 |
drop table if exists client_site;
|
125 |
create or replace table client_site (
|
143 |
create table client_site (
|
126 |
client_site_id int unsigned not null auto_increment,
|
144 |
client_site_id int unsigned not null auto_increment,
|
127 |
client_id int unsigned not null references client( client_id ),
|
145 |
client_id int unsigned not null references client( client_id ),
|
128 |
site_id int unsigned not null references site( site_id ),
|
146 |
site_id int unsigned not null references site( site_id ),
|
129 |
added_date date not null default now() comment 'Date record added to database',
|
147 |
added_date date not null comment 'Date record added to database',
|
130 |
removed_date date null default null comment 'Date record marked as removed',
|
148 |
removed_date date default null comment 'Date record marked as removed',
|
131 |
primary key (client_id,asset_id)
|
149 |
primary key (client_site_id)
|
132 |
) comment 'A client owns a site';
|
150 |
) comment 'A client owns a site';
|
133 |
|
151 |
|
- |
|
152 |
/*
|
- |
|
153 |
A device is owned by a client. Ownership can be tracked by removing
|
- |
|
154 |
a device from one client and adding it to another.
|
- |
|
155 |
*/
|
- |
|
156 |
drop table if exists client_device;
|
134 |
create or replace table client_asset (
|
157 |
create table client_device (
|
135 |
client_asset_id int unsigned not null auto_increment,
|
158 |
client_device_id int unsigned not null auto_increment,
|
136 |
client_id int unsigned not null references client( client_id ),
|
159 |
client_id int unsigned not null references client( client_id ),
|
137 |
asset_id int unsigned not null references asset( asset_id ),
|
160 |
device_id int unsigned not null references device( device_id ),
|
138 |
added_date date not null default now() comment 'Date record added to database',
|
161 |
added_date date not null comment 'Date record added to database',
|
139 |
removed_date date null default null comment 'Date record marked as removed',
|
162 |
removed_date date default null comment 'Date record marked as removed',
|
140 |
primary key (client_asset_id)
|
163 |
primary key (client_device_id)
|
141 |
) comment 'Links client and asset tables';
|
164 |
) comment 'Links client and device tables';
|
142 |
|
165 |
|
- |
|
166 |
/*
|
- |
|
167 |
A device is at a location. Location history can be tracked by removing
|
- |
|
168 |
and adding a device to a new location
|
- |
|
169 |
*/
|
- |
|
170 |
drop table if exists site_device;
|
143 |
create or replace table site_asset (
|
171 |
create table site_device (
|
144 |
site_asset_id int unsigned not null auto_increment,
|
172 |
site_device_id int unsigned not null auto_increment,
|
145 |
site_id int unsigned not null references site( site_id ),
|
173 |
site_id int unsigned not null references site( site_id ),
|
146 |
asset_id int unsigned not null references asset( asset_id ),
|
174 |
device_id int unsigned not null references device( device_id ),
|
147 |
added_date date not null default now() comment 'Date record added to database',
|
175 |
added_date date not null comment 'Date record added to database',
|
148 |
removed_date date null default null comment 'Date record marked as removed',
|
176 |
removed_date date default null comment 'Date record marked as removed',
|
149 |
primary key ( site_asset_id )
|
177 |
primary key ( site_device_id )
|
150 |
) comment 'Links site and asset tables';
|
178 |
) comment 'Links site and device tables';
|
151 |
|
179 |
|
152 |
/* add some indexes */
|
180 |
/* add some indexes */
|
153 |
alter table asset add index (added_date,removed_date);
|
181 |
alter table device add index (added_date,removed_date);
|
154 |
alter table asset add index (part_of);
|
- |
|
155 |
alter table asset add index (removed_date);
|
182 |
alter table device add index (removed_date);
|
156 |
alter table asset add index (site_id);
|
183 |
alter table device add index (name);
|
157 |
alter table asset add index (name);
|
184 |
alter table device add index (device_type_id);
|
- |
|
185 |
|
158 |
alter table asset add index (asset_type_id);
|
186 |
alter table client_site add index ( client_id,site_id );
|
159 |
|
187 |
|
160 |
alter table site add index (removed_date);
|
188 |
alter table site add index (removed_date);
|
161 |
|
189 |
|
162 |
alter table client add index (removed_date);
|
190 |
alter table client add index (removed_date);
|
163 |
|
191 |
|
164 |
/* some convenience views */
|
192 |
/* preload some data */
|
- |
|
193 |
|
- |
|
194 |
/* basic menu; home and logout */
|
- |
|
195 |
insert into _menu values ( null, null, 'Home Page', 'index.html' );
|
- |
|
196 |
insert into _menu values ( null, null, 'Log Out', 'logout.html' );
|
165 |
|
197 |
|
166 |
/* this will combine asset, client and site */
|
198 |
/* one user, admin, with no password for initial access */
|
167 |
create or replace view view_current_asset_full as
|
- |
|
168 |
select
|
- |
|
169 |
asset.name 'asset',
|
- |
|
170 |
client.name 'client',
|
- |
|
171 |
site.name 'site'
|
- |
|
172 |
from
|
- |
|
173 |
asset
|
- |
|
174 |
join client using( client_id )
|
- |
|
175 |
join site using ( site_id )
|
- |
|
176 |
where
|
- |
|
177 |
client.removed_date is null
|
- |
|
178 |
and site.removed_date is null
|
- |
|
179 |
and asset.removed_date is null;
|
199 |
insert into _user( username,added_date) values ( 'admin', now() );
|
180 |
|
- |
|
181 |
create or replace view view_asset_name as
|
- |
|
182 |
select
|
- |
|
183 |
asset_id 'asset_id',
|
- |
|
184 |
null 'alias_id',
|
- |
|
185 |
name 'name'
|
- |
|
186 |
from asset
|
- |
|
187 |
union
|
- |
|
188 |
select
|
- |
|
189 |
asset_id 'asset_id',
|
- |
|
190 |
alias_id 'alias_id',
|
- |
|
191 |
name 'name'
|
- |
|
192 |
from
|
- |
|
193 |
asset_alias
|
- |
|
194 |
where
|
- |
|
195 |
source = 'asset';
|
- |
|
196 |
|
- |
|
197 |
create or replace view view_client_name as
|
- |
|
198 |
select
|
- |
|
199 |
client_id 'client_id',
|
- |
|
200 |
null 'alias_id',
|
- |
|
201 |
name 'name'
|
- |
|
202 |
from client
|
- |
|
203 |
union
|
- |
|
204 |
select
|
- |
|
205 |
id 'client_id',
|
- |
|
206 |
alias_id 'alias_id',
|
- |
|
207 |
name 'name'
|
- |
|
208 |
from
|
- |
|
209 |
alias
|
- |
|
210 |
where
|
- |
|
211 |
source = 'client';
|
- |
|
212 |
|
- |
|
213 |
create or replace view view_site_name as
|
- |
|
214 |
select
|
- |
|
215 |
site_id 'site_id',
|
- |
|
216 |
null 'alias_id',
|
- |
|
217 |
name 'name'
|
- |
|
218 |
from site
|
- |
|
219 |
union
|
- |
|
220 |
select
|
- |
|
221 |
id 'site_id',
|
- |
|
222 |
alias_id 'alias_id',
|
- |
|
223 |
name 'name'
|
- |
|
224 |
from
|
- |
|
225 |
alias
|
- |
|
226 |
where
|
- |
|
227 |
source = 'site';
|
- |
|
228 |
|
200 |
|
- |
|
201 |
insert into device_type (name,flags, added_date ) values ( 'Computer',1,now() );
|