alter table role_x_rights drop constraint fk_right_role;
alter table role_x_rights drop constraint fk_role_right;
alter table category drop constraint fk_category_categorytype;
alter table category drop constraint fk_category_parentcategory;
alter table category drop constraint fk_topic_childcategories;
alter table media drop constraint fk_media_user;
alter table media_x_media drop constraint fk_mediaxmedia_sourcemedia;
alter table media_x_media drop constraint fk_mediaxmedia_relatedmedia;
alter table media_x_media drop constraint fk_mediaxmedia_mediarelationtype;
alter table uploaded_media drop constraint fk_uploadedmedia_mediafolder;
alter table uploaded_media drop constraint fk_uploadedmedia_mediatype;
alter table uploaded_media drop constraint fk_uploadedmedia_media;
alter table article drop constraint fk_article_language;
alter table article drop constraint fk_article_articlestatus;
alter table article drop constraint fk_article_media;
alter table article drop constraint fk_article_lockinguser;
alter table article_x_category drop constraint fk_article_category_2;
alter table article_x_category drop constraint fk_articlexcategory_category;
alter table article_x_category drop constraint fk_article_category;
alter table article_x_category drop constraint fk_category_article;
alter table article_x_category drop constraint fk_articlexcategory_article_type;
alter table article_x_category drop constraint fk_articlexcategory;
alter table users drop constraint fk_user_role;
alter table users drop constraint fk_role_user;
drop table role_x_rights;
drop table category_type;
drop table media_type;
drop table rights;
drop table category;
drop table media;
drop table roles;
drop table article_type;
drop table media_x_media;
drop table language;
drop table uploaded_media;
drop table article;
drop table media_relation_type;
drop table article_x_category;
drop table breaking_news_items;
drop table comment_status;
drop table users;
drop table media_folder;
drop table messages;
drop sequence language_id_seq;
drop sequence users_id_seq;
drop sequence media_relation_type_id_seq;
drop sequence category_type_id_seq;
drop sequence breaking_id_seq;
drop sequence media_type_id_seq;
drop sequence article_type_id_seq;
drop sequence right_id_seq;
drop sequence comment_status_id_seq;
drop sequence media_folder_id_seq;
drop sequence media_id_seq;
drop sequence category_id_seq;
drop sequence role_id_seq;
create table role_x_rights (
   role_id int4 not null,
   right_id int4 not null,
   primary key (role_id, right_id)
);
create table category_type (
   id int4 not null,
   name varchar(40) not null,
   primary key (id)
);
create table media_type (
   id int4 not null,
   name varchar(80) not null,
   mime_type varchar(40) not null,
   classname varchar(80) not null,
   tablename varchar(80) not null,
   dcname varchar(20),
   primary key (id)
);
create table rights (
   id int4 not null,
   name varchar(80) not null,
   description varchar(255),
   primary key (id)
);
create table category (
   id int4 not null,
   title varchar(80) not null,
   description varchar(255),
   filename varchar(20) not null,
   category_type int4,
   parent_id int4,
   primary key (id)
);
create table media (
   id int4 not null,
   title varchar(255),
   description text,
   creator varchar(80),
   creator_main_url varchar(255),
   creator_email varchar(80),
   creator_address varchar(80),
   creator_phone varchar(80),
   internal_comment varchar(255),
   is_produced bool not null,
   creation_date timestamp not null,
   last_change timestamp,
   to_publisher int4,
   primary key (id)
);
create table roles (
   id int4 not null,
   name varchar(16) not null unique,
   description varchar(255) not null,
   primary key (id)
);
create table article_type (
   id int4 not null,
   name varchar(40) not null,
   primary key (id)
);
create table media_x_media (
   source_media_id int4 not null,
   media_relation_type_id int4 not null,
   related_media_id int4 not null,
   primary key (source_media_id, related_media_id)
);
create table language (
   id int4 not null,
   name varchar(40) not null,
   code varchar(2) not null,
   primary key (id)
);
create table uploaded_media (
   media_id int4 not null,
   publish_server varchar(255),
   publish_path varchar(255),
   storage_path varchar(255),
   icon_produced bool not null,
   icon_path varchar(255),
   size int4,
   to_media_folder int4,
   to_media_type int4,
   primary key (media_id)
);
create table article (
   media_id int4 not null,
   date varchar(8) not null,
   subtitle varchar(255),
   edittitle varchar(255),
   content text not null,
   is_html bool not null,
   to_article_status int4,
   to_locking_user int4,
   to_language int4,
   primary key (media_id)
);
create table media_relation_type (
   id int4 not null,
   name varchar(255),
   primary key (id)
);
create table article_x_category (
   article_type_id int4 not null,
   category_id int4 not null,
   article_id int4 not null,
   primary key (category_id, article_id)
);
create table breaking_news_items (
   id int4 not null,
   text varchar(255) not null,
   creationDate timestamp not null,
   primary key (id)
);
create table comment_status (
   id int4 not null,
   name varchar(40) not null,
   primary key (id)
);
create table users (
   id int4 not null,
   login varchar(16) not null unique,
   password varchar(255) not null,
   internal_comment varchar(255),
   is_disabled bool not null,
   email varchar(255) not null,
   profile varchar(255),
   creationDate timestamp not null,
   lastlogin timestamp,
   to_role int4,
   primary key (id)
);
create table media_folder (
   id int4 not null,
   name varchar(255) not null,
   date varchar(8) not null,
   place varchar(80),
   keywords varchar(255),
   internal_comment varchar(255),
   creationDate timestamp not null,
   last_change timestamp,
   primary key (id)
);
create table messages (
   id int4 not null,
   title varchar(30),
   description varchar(255) not null,
   creator varchar(30) not null,
   creation_date timestamp not null,
   primary key (id)
);
alter table role_x_rights add constraint fk_right_role foreign key (right_id) references rights;
alter table role_x_rights add constraint fk_role_right foreign key (role_id) references roles;
alter table category add constraint fk_category_categorytype foreign key (category_type) references category_type;
alter table category add constraint fk_category_parentcategory foreign key (parent_id) references category;
alter table category add constraint fk_topic_childcategories foreign key (parent_id) references category;
alter table media add constraint fk_media_user foreign key (to_publisher) references users;
alter table media_x_media add constraint fk_mediaxmedia_sourcemedia foreign key (source_media_id) references media;
alter table media_x_media add constraint fk_mediaxmedia_relatedmedia foreign key (related_media_id) references media;
alter table media_x_media add constraint fk_mediaxmedia_mediarelationtype foreign key (media_relation_type_id) references media_relation_type;
alter table uploaded_media add constraint fk_uploadedmedia_mediafolder foreign key (to_media_folder) references media_folder;
alter table uploaded_media add constraint fk_uploadedmedia_mediatype foreign key (to_media_type) references media_type;
alter table uploaded_media add constraint fk_uploadedmedia_media foreign key (media_id) references media;
alter table article add constraint fk_article_language foreign key (to_language) references language;
alter table article add constraint fk_article_articlestatus foreign key (to_article_status) references comment_status;
alter table article add constraint fk_article_media foreign key (media_id) references media;
alter table article add constraint fk_article_lockinguser foreign key (to_locking_user) references users;
alter table article_x_category add constraint fk_article_category_2 foreign key (article_id) references article;
alter table article_x_category add constraint fk_articlexcategory_category foreign key (category_id) references category;
alter table article_x_category add constraint fk_article_category foreign key (article_id) references article;
alter table article_x_category add constraint fk_category_article foreign key (category_id) references category;
alter table article_x_category add constraint fk_articlexcategory_article_type foreign key (article_type_id) references article_type;
alter table article_x_category add constraint fk_articlexcategory foreign key (article_type_id, category_id) references article_x_category;
alter table users add constraint fk_user_role foreign key (to_role) references roles;
alter table users add constraint fk_role_user foreign key (to_role) references roles;
create sequence language_id_seq;
create sequence users_id_seq;
create sequence media_relation_type_id_seq;
create sequence category_type_id_seq;
create sequence breaking_id_seq;
create sequence media_type_id_seq;
create sequence article_type_id_seq;
create sequence right_id_seq;
create sequence comment_status_id_seq;
create sequence media_folder_id_seq;
create sequence media_id_seq;
create sequence category_id_seq;
create sequence role_id_seq;
