CREATE SEQUENCE sepa_mandat_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;

CREATE TABLE sepa_mandat (
	sepa_mandat_id integer NOT NULL,
    bp_id integer NOT NULL,
    bank_customer character varying(50) NOT NULL,
    bank_name character varying(50),
    bank_iban character varying(40) NOT NULL,
    bank_bic character varying(40) NOT NULL,
    mandat_ref character varying(40) NOT NULL,
    mandat_signed date NOT NULL,
    mandat_since date NOT NULL,
    mandat_used date,
    mandat_until date
);

ALTER TABLE ONLY sepa_mandat ALTER COLUMN sepa_mandat_id SET DEFAULT nextval('sepa_mandat_id_seq'::regclass);

ALTER TABLE ONLY sepa_mandat
    ADD CONSTRAINT pk_sepa_mandat PRIMARY KEY (sepa_mandat_id);

ALTER TABLE ONLY sepa_mandat
    ADD CONSTRAINT dateschk1 CHECK (mandat_signed <= mandat_since AND mandat_since <= mandat_until);

ALTER TABLE ONLY sepa_mandat
    ADD CONSTRAINT dateschk2 CHECK (mandat_since <= mandat_until AND mandat_since <= mandat_used AND mandat_used <= mandat_until);

--
-- Name: basecomponent; Type: TABLE; Schema: public; Owner: -; Tablespace: 
--

CREATE TABLE basecomponent (
    basecomponent_id integer DEFAULT nextval(('"basecomponent_basecomponent_seq"'::text)::regclass) NOT NULL,
    basecomponent_code character varying(10) NOT NULL,
    description character varying(100) NOT NULL,
    sorting integer NOT NULL,
    valid boolean NOT NULL
);


--
-- Name: basecomponent_basecomponent_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE basecomponent_basecomponent_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- Name: basepacket; Type: TABLE; Schema: public; Owner: -; Tablespace: 
--

CREATE SEQUENCE basepacket_basepacket_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;

CREATE TABLE basepacket (
    basepacket_id integer DEFAULT nextval(('"basepacket_basepacket_id_seq"'::text)::regclass) NOT NULL,
    basepacket_code character varying(10) NOT NULL,
    description character varying(100) NOT NULL,
    article_number integer NOT NULL,
    sorting integer NOT NULL,
    valid boolean NOT NULL
);

ALTER TABLE ONLY basepacket
    ADD CONSTRAINT pk_basepacket PRIMARY KEY (basepacket_id);

--
-- Name: business_partner; Type: TABLE; Schema: public; Owner: -; Tablespace: 
--

CREATE TABLE business_partner (
    bp_id integer DEFAULT nextval(('"business_partner_bp_id_seq"'::text)::regclass) NOT NULL,
    member_id integer NOT NULL,
    member_code character varying(20) NOT NULL,
    member_since date,
    member_until date,
    member_role character varying(100),
    author_contract date,
    nondisc_contract date,
    shares_updated date,
    shares_signed integer NOT NULL,
    uid_vat character varying(20),
    free boolean NOT NULL,
    indicator_vat character varying(20) NOT NULL,
    exempt_vat boolean NOT NULL,
    CONSTRAINT ckc_member_id_business CHECK (((member_id >= 10000) AND (member_id <= 99999))),
    CONSTRAINT ckc_shares_signed_business CHECK ((shares_signed >= 0)),
    CONSTRAINT ckt_business_partner CHECK ( ( ((member_since IS NULL) AND (member_until IS NULL)) OR ((member_since IS NOT NULL) AND (member_until IS NULL)) OR ((member_since IS NOT NULL) AND (member_until IS NOT NULL) AND (member_since < member_until)) ) AND ((member_code)::text ~~ 'hsh00-%'::text)),
    UNIQUE (bp_id)
);


--
-- Name: business_partner_bp_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE business_partner_bp_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;

ALTER TABLE ONLY sepa_mandat
    ADD CONSTRAINT fk_bank_acc_reference_business FOREIGN KEY (bp_id) REFERENCES business_partner(bp_id) ON UPDATE RESTRICT ON DELETE RESTRICT;



--
-- Name: component; Type: TABLE; Schema: public; Owner: -; Tablespace: 
--

CREATE SEQUENCE component_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;

CREATE TABLE component (
	component_id integer DEFAULT nextval(('"component_id_seq"'::text)::regclass) NOT NULL,
    basepacket_id integer NOT NULL,
    basecomponent_id integer NOT NULL,
    article_number integer NOT NULL,
    min_quantity integer NOT NULL,
    max_quantity integer NOT NULL,
    default_quantity integer NOT NULL,
    increment_quantity integer NOT NULL,
    include_quantity integer NOT NULL,
    admin_only boolean NOT NULL,
    CONSTRAINT ckt_component CHECK (
    	(0 <= min_quantity) AND 
    	(min_quantity <= default_quantity) AND 
    	(default_quantity <= max_quantity) AND 
    	(include_quantity <= default_quantity) AND 
    	(0 <= include_quantity) AND 
    	(mod(min_quantity, increment_quantity) = 0) AND 
    	(mod(max_quantity, increment_quantity) = 0) AND 
    	(mod(default_quantity, increment_quantity) = 0) AND 
    	(mod(include_quantity, increment_quantity) = 0)
    )
);


--
-- Name: contact; Type: TABLE; Schema: public; Owner: -; Tablespace: 
--

CREATE TABLE contact (
    contact_id integer DEFAULT nextval(('"contact_contact_id_seq"'::text)::regclass) NOT NULL,
    bp_id integer NOT NULL,
    salut character varying(30),
    first_name character varying(40) NOT NULL,
    last_name character varying(40) NOT NULL,
    title character varying(20),
    firma character varying(120),
    co character varying(50),
    street character varying(50),
    zipcode character varying(10),
    city character varying(40),
    country character varying(30),
    phone_private character varying(30),
    phone_office character varying(30),
    phone_mobile character varying(30),
    fax character varying(30),
    email character varying(100) NOT NULL,
    CONSTRAINT ckc_email_contact CHECK (((email)::text ~~ '%@%.%'::text))
);

ALTER TABLE ONLY contact
    ADD CONSTRAINT pk_contact PRIMARY KEY (contact_id);
    
--
-- Name: contact_contact_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE contact_contact_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;

CREATE TABLE contactrole_ref (
    contact_id integer NOT NULL,
    role character varying(40) NOT NULL
);

ALTER TABLE ONLY contactrole_ref
    ADD CONSTRAINT pk_contactrole_ref PRIMARY KEY (contact_id, role);
    
CREATE TABLE role ( 
    role_name character varying(40) NOT NULL
);

ALTER TABLE ONLY role
    ADD CONSTRAINT pk_role PRIMARY KEY (role_name);

INSERT INTO role VALUES ('billing'), ('operations');

ALTER TABLE ONLY contactrole_ref
    ADD CONSTRAINT fk_contactrole_ref_role FOREIGN KEY (role) REFERENCES role(role_name);

ALTER TABLE ONLY contactrole_ref
    ADD CONSTRAINT fk_contactrole_ref_contact FOREIGN KEY (contact_id) REFERENCES contact(contact_id);
    
INSERT INTO contactrole_ref (contact_id, role) 
	( SELECT contact_id, 'billing' FROM contact );
	
INSERT INTO contactrole_ref (contact_id, role) 
	( SELECT contact_id, 'operations' FROM contact );

CREATE TABLE price_list (
    id serial primary key,                                                                   
    name character varying(20)
);       

ALTER TABLE price_list
	ADD CONSTRAINT price_list_uniq_name UNIQUE (name);
	
CREATE TABLE pricelist_ref (
    bp_id integer NOT NULL,
    price_list character varying(40) NOT NULL
);

ALTER TABLE ONLY pricelist_ref
    ADD CONSTRAINT fk_pricelist_ref_bp FOREIGN KEY (bp_id) REFERENCES business_partner(bp_id) ON UPDATE RESTRICT ON DELETE CASCADE;
	
ALTER TABLE ONLY pricelist_ref
    ADD CONSTRAINT fk_pricelist_ref_pricelist FOREIGN KEY (price_list) REFERENCES price_list(name) ON UPDATE RESTRICT ON DELETE CASCADE;
	

--
-- Name: price
--

CREATE TABLE price (                                                                
    id serial primary key,
    article_number integer NOT NULL,
    price decimal(10, 2) NOT NULL,
    vat decimal(4,2) NOT NULL,
    price_list integer references price_list(id)                                             
);

--
-- Name: database_database_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE database_database_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- Name: database; Type: TABLE; Schema: public; Owner: -; Tablespace: 
--

CREATE TABLE database (
    database_id integer DEFAULT nextval('database_database_id_seq'::regclass) NOT NULL,
    engine character varying(12) NOT NULL,
    packet_id integer NOT NULL,
    name character varying(64) NOT NULL,
    owner character varying(24) NOT NULL,
    encoding character varying(12) NOT NULL
);


--
-- Name: dbuser_dbuser_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE dbuser_dbuser_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- Name: database_user; Type: TABLE; Schema: public; Owner: -; Tablespace: 
--

CREATE TABLE database_user (
    dbuser_id integer DEFAULT nextval('dbuser_dbuser_id_seq'::regclass) NOT NULL,
    engine character varying(12) NOT NULL,
    packet_id integer NOT NULL,
    name character varying(64) NOT NULL
);


--
-- Name: domain_domain_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE domain_domain_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- Name: domain; Type: TABLE; Schema: public; Owner: -; Tablespace: 
--

CREATE TABLE domain (
    domain_name character varying(256) NOT NULL,
    domain_since date,
    domain_dns_master character varying(64),
    domain_id integer DEFAULT nextval('domain_domain_id_seq'::regclass) NOT NULL,
    domain_owner integer NOT NULL,
    valid_subdomain_names character varying(256) DEFAULT 'www' NOT NULL,
    passenger_python character varying(256) NULL,
    passenger_nodejs character varying(256) NULL,
    passenger_ruby character varying(256) NULL,
    fcgi_php_bin character varying(256) NULL,
    proxy_http_port integer DEFAULT NULL,
    proxy_websocket_port integer DEFAULT NULL
);


--
-- Name: emailaddr_emailaddr_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE emailaddr_emailaddr_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- Name: emailaddr; Type: TABLE; Schema: public; Owner: -; Tablespace: 
--

CREATE TABLE emailaddr (
    emailaddr_id integer DEFAULT nextval('emailaddr_emailaddr_id_seq'::regclass) NOT NULL,
    localpart character varying(64) NOT NULL,
    domain_id integer NOT NULL,
    target text,
    subdomain character varying(64)
);


--
-- Name: emailalias_emailalias_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE emailalias_emailalias_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- Name: emailalias; Type: TABLE; Schema: public; Owner: -; Tablespace: 
--

CREATE TABLE emailalias (
    emailalias_id integer DEFAULT nextval('emailalias_emailalias_id_seq'::regclass) NOT NULL,
    pac_id integer NOT NULL,
    target text NOT NULL,
    name character varying(96) NOT NULL
);


--
-- Name: hive; Type: TABLE; Schema: public; Owner: -; Tablespace: 
--

CREATE TABLE hive (
    hive_id integer DEFAULT nextval(('"hive_hive_id_seq"'::text)::regclass) NOT NULL,
    hive_name character varying(3) NOT NULL,
    inet_addr_id integer NOT NULL,
    description character varying(100)
);


--
-- Name: hive_hive_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE hive_hive_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- Name: inet_addr; Type: TABLE; Schema: public; Owner: -; Tablespace: 
--

CREATE TABLE inet_addr (
    inet_addr_id integer DEFAULT nextval(('"inet_addr_inet_addr_id_seq"'::text)::regclass) NOT NULL,
    inet_addr inet NOT NULL,
    description character varying(100),
    CONSTRAINT ckc_inet_addr_inet_add CHECK ((masklen(inet_addr) = 32))
);


--
-- Name: inet_addr_inet_addr_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE inet_addr_inet_addr_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- Name: packet; Type: TABLE; Schema: public; Owner: -; Tablespace: 
--

CREATE SEQUENCE packet_packet_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;

CREATE TABLE packet (
    packet_id integer DEFAULT nextval(('"packet_packet_id_seq"'::text)::regclass) NOT NULL,
    basepacket_id integer NOT NULL,
    packet_name character varying(6) NOT NULL,
    bp_id integer NOT NULL,
    hive_id integer,
    created date NOT NULL,
    cancelled date,
    free boolean NOT NULL,
    cur_inet_addr_id integer,
    old_inet_addr_id integer,
    CONSTRAINT ckt_packet CHECK (((cancelled IS NULL) OR (cancelled > created)))
);

ALTER TABLE ONLY packet
    ADD CONSTRAINT base_packet_ref FOREIGN KEY (basepacket_id) REFERENCES basepacket(basepacket_id);


--
-- Name: packet_component; Type: TABLE; Schema: public; Owner: -; Tablespace: 
--

CREATE SEQUENCE packet_component_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;

CREATE TABLE packet_component (
	packet_component_id integer DEFAULT nextval(('"packet_component_id_seq"'::text)::regclass) NOT NULL,
    basecomponent_id integer NOT NULL,
    packet_id integer NOT NULL,
    quantity integer NOT NULL,
    created date,
    cancelled date,
    CONSTRAINT ckt_packet_component CHECK (((cancelled IS NULL) OR (cancelled > created)))
);


--
-- Name: queue_task; Type: TABLE; Schema: public; Owner: -; Tablespace: 
--

CREATE TABLE queue_task (
    task_id integer DEFAULT nextval(('"queue_task_id_seq"'::text)::regclass) NOT NULL,
    proc bytea,
    exception text,
    started timestamp without time zone NOT NULL,
    finished timestamp without time zone,
    title character varying(192),
    details text,
    user_id integer
);


--
-- Name: queue_task_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE queue_task_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- Name: unixuser_unixuser_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE unixuser_unixuser_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;


--
-- Name: unixuser; Type: TABLE; Schema: public; Owner: -; Tablespace: 
--

CREATE TABLE unixuser (
    unixuser_id integer DEFAULT nextval('unixuser_unixuser_id_seq'::regclass) NOT NULL,
    name character varying(64) NOT NULL,
    comment character varying(128),
    shell character varying(64) NOT NULL,
    homedir character varying(128) NOT NULL,
    locked boolean NOT NULL,
    packet_id integer NOT NULL,
    userid integer NOT NULL,
    quota_softlimit integer DEFAULT 0 NOT NULL,
    quota_hardlimit integer DEFAULT 0,
	storage_softlimit integer DEFAULT 0 NOT NULL,
	storage_hardlimit integer DEFAULT 0,
    CONSTRAINT unixuser_userid CHECK ((userid >= 10000))
);


--
-- Name: database_uniq; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
--

ALTER TABLE ONLY database
    ADD CONSTRAINT database_uniq UNIQUE (engine, name);


--
-- Name: database_user_uniq; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
--

ALTER TABLE ONLY database_user
    ADD CONSTRAINT database_user_uniq UNIQUE (engine, name);


--
-- Name: emailaddr_uniq; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
--

ALTER TABLE ONLY emailaddr
    ADD CONSTRAINT emailaddr_uniq UNIQUE (localpart, subdomain, domain_id);


--
-- Name: emailalias_uniq; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
--

ALTER TABLE ONLY emailalias
    ADD CONSTRAINT emailalias_uniq UNIQUE (name);


--
-- Name: pk_basecomponent; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
--

ALTER TABLE ONLY basecomponent
    ADD CONSTRAINT pk_basecomponent PRIMARY KEY (basecomponent_id);


--
-- Name: [REDACTED] Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
--

ALTER TABLE ONLY business_partner
    ADD CONSTRAINT [REDACTED]PRIMARY KEY (bp_id);


--
-- Name: pk_component; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
--

ALTER TABLE ONLY component
    ADD CONSTRAINT pk_component PRIMARY KEY (component_id);

ALTER TABLE ONLY database
    ADD CONSTRAINT pk_database PRIMARY KEY (database_id);


--
-- Name: pk_database_user; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
--

ALTER TABLE ONLY database_user
    ADD CONSTRAINT pk_database_user PRIMARY KEY (dbuser_id);


--
-- Name: pk_hive; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
--

ALTER TABLE ONLY hive
    ADD CONSTRAINT pk_hive PRIMARY KEY (hive_id);


--
-- Name: pk_inet_addr; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
--

ALTER TABLE ONLY inet_addr
    ADD CONSTRAINT pk_inet_addr PRIMARY KEY (inet_addr_id);


--
-- Name: pk_packet; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
--

ALTER TABLE ONLY packet
    ADD CONSTRAINT pk_packet PRIMARY KEY (packet_id);


--
-- Name: [REDACTED] Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
--

ALTER TABLE ONLY packet_component
    ADD CONSTRAINT [REDACTED]PRIMARY KEY (packet_component_id);


--
-- Name: pk_unixuser; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
--

ALTER TABLE ONLY unixuser
    ADD CONSTRAINT pk_unixuser PRIMARY KEY (unixuser_id);


--
-- Name: unixuser_name_key; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: 
--

ALTER TABLE ONLY unixuser
    ADD CONSTRAINT unixuser_name_key UNIQUE (name);


--
-- Name: basecomponent_in_1; Type: INDEX; Schema: public; Owner: -; Tablespace: 
--

CREATE UNIQUE INDEX basecomponent_in_1 ON basecomponent USING btree (basecomponent_code);


--
-- Name: basepacket_in_1; Type: INDEX; Schema: public; Owner: -; Tablespace: 
--

CREATE UNIQUE INDEX basepacket_in_1 ON basepacket USING btree (basepacket_code);


--
-- Name: component_in_1; Type: INDEX; Schema: public; Owner: -; Tablespace: 
--

CREATE INDEX component_in_1 ON component USING btree (basecomponent_id);


--
-- Name: contact_in_1; Type: INDEX; Schema: public; Owner: -; Tablespace: 
--

CREATE INDEX contact_in_1 ON contact USING btree (bp_id);


--
-- Name: customer_in_1; Type: INDEX; Schema: public; Owner: -; Tablespace: 
--

CREATE UNIQUE INDEX customer_in_1 ON business_partner USING btree (member_code);


--
-- Name: customer_in_2; Type: INDEX; Schema: public; Owner: -; Tablespace: 
--

CREATE UNIQUE INDEX customer_in_2 ON business_partner USING btree (member_id);


--
-- Name: database_unique_owner; Type: INDEX; Schema: public; Owner: -; Tablespace: 
--

CREATE UNIQUE INDEX database_unique_owner ON database_user USING btree (name, engine);


--
-- Name: domain_unique_id; Type: INDEX; Schema: public; Owner: -; Tablespace: 
--

CREATE UNIQUE INDEX domain_unique_id ON domain USING btree (domain_id);


--
-- Name: domain_unique_name; Type: INDEX; Schema: public; Owner: -; Tablespace: 
--

CREATE UNIQUE INDEX domain_unique_name ON domain USING btree (domain_name);


--
-- Name: emailaddr_uniq2; Type: INDEX; Schema: public; Owner: -; Tablespace: 
--

CREATE UNIQUE INDEX emailaddr_uniq2 ON emailaddr USING btree (localpart, domain_id) WHERE (subdomain IS NULL);


--
-- Name: hive_in_1; Type: INDEX; Schema: public; Owner: -; Tablespace: 
--

CREATE UNIQUE INDEX hive_in_1 ON hive USING btree (hive_name);


--
-- Name: inet_addr_in_1; Type: INDEX; Schema: public; Owner: -; Tablespace: 
--

CREATE UNIQUE INDEX inet_addr_in_1 ON inet_addr USING btree (inet_addr);


--
-- Name: packet_component_in_1; Type: INDEX; Schema: public; Owner: -; Tablespace: 
--

CREATE INDEX packet_component_in_1 ON packet_component USING btree (packet_id);


--
-- Name: packet_component_in_2; Type: INDEX; Schema: public; Owner: -; Tablespace: 
--

CREATE INDEX packet_component_in_2 ON packet_component USING btree (basecomponent_id);


--
-- Name: packet_in_1; Type: INDEX; Schema: public; Owner: -; Tablespace: 
--

CREATE UNIQUE INDEX packet_in_1 ON packet USING btree (packet_name);


--
-- Name: packet_in_2; Type: INDEX; Schema: public; Owner: -; Tablespace: 
--

CREATE INDEX packet_in_2 ON packet USING btree (bp_id);


--
-- Name: unique_task_id; Type: INDEX; Schema: public; Owner: -; Tablespace: 
--

CREATE UNIQUE INDEX unique_task_id ON queue_task USING btree (task_id);


--
-- Name: database_owner; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY database
    ADD CONSTRAINT database_owner FOREIGN KEY (owner, engine) REFERENCES database_user(name, engine);


--
-- Name: database_packet_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY database
    ADD CONSTRAINT database_packet_id_fkey FOREIGN KEY (packet_id) REFERENCES packet(packet_id) DEFERRABLE;


--
-- Name: dbuser_packet_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY database_user
    ADD CONSTRAINT dbuser_packet_id_fkey FOREIGN KEY (packet_id) REFERENCES packet(packet_id) DEFERRABLE;


--
-- Name: domain_owner_chk; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY domain
    ADD CONSTRAINT domain_owner_chk FOREIGN KEY (domain_owner) REFERENCES unixuser(unixuser_id) MATCH FULL;


--
-- Name: email_domain; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY emailaddr
    ADD CONSTRAINT email_domain FOREIGN KEY (domain_id) REFERENCES domain(domain_id);


--
-- Name: emailalias_pac_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY emailalias
    ADD CONSTRAINT emailalias_pac_id_fkey FOREIGN KEY (pac_id) REFERENCES packet(packet_id) DEFERRABLE;


--
-- Name: fk_bcomp_bpack; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY component
    ADD CONSTRAINT fk_bcomp_bpack FOREIGN KEY (basepacket_id) REFERENCES basepacket(basepacket_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: fk_comp_pack; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY packet_component
    ADD CONSTRAINT fk_comp_pack FOREIGN KEY (packet_id) REFERENCES packet(packet_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: fk_contact_reference_business; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY contact
    ADD CONSTRAINT fk_contact_reference_business FOREIGN KEY (bp_id) REFERENCES business_partner(bp_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: fk_hive_inet; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY hive
    ADD CONSTRAINT fk_hive_inet FOREIGN KEY (inet_addr_id) REFERENCES inet_addr(inet_addr_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: fk_pac_cur_inet; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY packet
    ADD CONSTRAINT fk_pac_cur_inet FOREIGN KEY (cur_inet_addr_id) REFERENCES inet_addr(inet_addr_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: fk_pac_old_inet; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY packet
    ADD CONSTRAINT fk_pac_old_inet FOREIGN KEY (old_inet_addr_id) REFERENCES inet_addr(inet_addr_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: fk_packet_bp; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY packet
    ADD CONSTRAINT fk_packet_bp FOREIGN KEY (bp_id) REFERENCES business_partner(bp_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: fk_packet_hive; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY packet
    ADD CONSTRAINT fk_packet_hive FOREIGN KEY (hive_id) REFERENCES hive(hive_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: fk_reference_13; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY component
    ADD CONSTRAINT fk_reference_13 FOREIGN KEY (basecomponent_id) REFERENCES basecomponent(basecomponent_id) ON UPDATE RESTRICT ON DELETE RESTRICT;


--
-- Name: queue_task_user_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY queue_task
    ADD CONSTRAINT queue_task_user_id_fkey FOREIGN KEY (user_id) REFERENCES unixuser(unixuser_id) ON DELETE SET NULL;

--
-- domain options
--

CREATE SEQUENCE domain_option_id_seq
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE
    CACHE 1;

CREATE TABLE domain_option (
    domain_option_id integer DEFAULT nextval(('"domain_option_id_seq"'::text)::regclass) NOT NULL,
    domain_option_name character varying(50) NOT NULL
);

ALTER TABLE ONLY domain_option
    ADD CONSTRAINT pk_domain_option PRIMARY KEY (domain_option_id);

CREATE UNIQUE INDEX domain_option_name_idx ON domain_option USING btree ( domain_option_name );
    
CREATE TABLE domain__domain_option (
	domain_option_id integer NOT NULL,
	domain_id integer NOT NULL
); 

ALTER TABLE ONLY domain__domain_option
    ADD CONSTRAINT [REDACTED]PRIMARY KEY (domain_option_id, domain_id);

ALTER TABLE ONLY domain__domain_option
    ADD CONSTRAINT domain_option_id_fkey FOREIGN KEY (domain_option_id) 
    	REFERENCES domain_option(domain_option_id) DEFERRABLE;

ALTER TABLE ONLY domain__domain_option
    ADD CONSTRAINT domain_id_fkey FOREIGN KEY (domain_id) 
    	REFERENCES domain(domain_id) DEFERRABLE;

