forward engineer with mysql workbench cannot create the schema - mysql-workbench

-- MySQL Workbench Forward Engineering
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema motivian
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema motivian
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `motivian` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `motivian` ;
-- -----------------------------------------------------
-- Table `motivian`.`user`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `motivian`.`user` ;
CREATE TABLE IF NOT EXISTS `motivian`.`user` (
`user_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`username` VARCHAR(100) NOT NULL,
`email` VARCHAR(100) NULL,
`password` VARCHAR(100) NOT NULL,
UNIQUE INDEX `username_UNIQUE` (`username` ASC),
PRIMARY KEY (`user_id`));
-- -----------------------------------------------------
-- Table `motivian`.`calculation`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `motivian`.`calculation` ;
CREATE TABLE IF NOT EXISTS `motivian`.`calculation` (
`category_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`calc` VARCHAR(100) NOT NULL,
`user_id` INT NULL,
`date_created` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`category_id`),
CONSTRAINT `user_id`
FOREIGN KEY ()
REFERENCES `motivian`.`user` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION);
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
This is auto generated with mysql workbench but the constraint could not be applied. I tried like this:
CONSTRAINT user_id
FOREIGN KEY (user_id)
REFERENCESmotivian.user(user_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
but also don't work.....
This is the error:
Executing SQL script in server
ERROR: Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')
REFERENCES `motivian`.`user` ()
ON DELETE NO ACTION
ON UPDATE NO A' at line 8
SQL Code:
CREATE TABLE IF NOT EXISTS `motivian`.`calculation` (
`category_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`calc` VARCHAR(100) NOT NULL,
`user_id` INT NULL,
`date_created` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`category_id`),
CONSTRAINT `user_id`
FOREIGN KEY ()
REFERENCES `motivian`.`user` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION)
SQL script execution finished: statements: 8 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch

Your FK definition is incomplete. Youd did not specify any column. Select your foreign key in the table editor and specify column pairs in the list right to it:

Related

NetBeans Generated REST Service works for XML requests, but not JSON

I am using NetBeans to help me build a REST web service. My steps taken have been the following:
New Project -> Web Application
New File -> Web Service -> RESTFul WebService from Database
Select the Datasource (which is a MySQL DB)
Everything generates, right click on project -> Test RESTFul
WebServices
When I want to test GET(application/XML), I can get a result returned fine, I can also get an XML response from a Jersey Client I made after the fact. But when I test the JSON functions, I always seem to get errors:
exception
javax.servlet.ServletException: org.glassfish.jersey.server.ContainerException: java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.persistence.jaxb.BeanValidationHelper
root cause
org.glassfish.jersey.server.ContainerException:
java.lang.NoClassDefFoundError: Could not initialize class
org.eclipse.persistence.jaxb.BeanValidationHelper
root cause
java.lang.NoClassDefFoundError: Could not initialize class org.eclipse.persistence.jaxb.BeanValidationHelper
My Database schema, which would really be the only thing I've written myself, is the following:
-- ****************** SqlDBM: MySQL ******************;
-- ***************************************************;
DROP TABLE `roles`;
DROP TABLE `orders`;
DROP TABLE `inventory`;
DROP TABLE `users`;
DROP TABLE `warehouses`;
DROP TABLE `inventory`;
DROP TABLE `addresses`;
DROP TABLE `products`;
-- ************************************** `addresses`
CREATE TABLE `addresses`
(
`addressID` INTEGER NOT NULL AUTO_INCREMENT ,
`streetNum` INTEGER NOT NULL ,
`streetName` VARCHAR(45) NOT NULL ,
`unitNum` INTEGER ,
`city` VARCHAR(45) NOT NULL ,
`province` VARCHAR(45) NOT NULL ,
`postalCode` VARCHAR(6) NOT NULL ,
PRIMARY KEY (`addressID`)
);
-- ************************************** `products`
CREATE TABLE `products`
(
`productID` INTEGER NOT NULL AUTO_INCREMENT ,
`productNO` VARCHAR(40) NOT NULL ,
`productName` VARCHAR(80) NOT NULL ,
`productDesc` VARCHAR(160) NOT NULL ,
`cost` REAL NOT NULL ,
`price` REAL NOT NULL ,
PRIMARY KEY (`productID`)
);
-- ************************************** `users`
CREATE TABLE `users`
(
`userID` INTEGER NOT NULL AUTO_INCREMENT ,
`firstName` VARCHAR(20) NOT NULL ,
`lastName` VARCHAR(20) NOT NULL ,
`dateOfBirth` DATE NOT NULL ,
`email` VARCHAR(45) NOT NULL ,
`password` CHAR(64) NOT NULL ,
`addressID` INTEGER NOT NULL ,
PRIMARY KEY (`userID`),
KEY `fkIdx_87` (`addressID`),
CONSTRAINT `FK_87` FOREIGN KEY `fkIdx_87` (`addressID`) REFERENCES `addresses` (`addressID`)
);
-- ************************************** `warehouses`
CREATE TABLE `warehouses`
(
`warehouseID` INTEGER NOT NULL AUTO_INCREMENT ,
`addressID` INTEGER NOT NULL ,
PRIMARY KEY (`warehouseID`),
KEY `fkIdx_58` (`addressID`),
CONSTRAINT `FK_58` FOREIGN KEY `fkIdx_58` (`addressID`) REFERENCES `addresses` (`addressID`)
);
-- ************************************** `inventory`
CREATE TABLE `inventory`
(
`id` INTEGER NOT NULL AUTO_INCREMENT ,
`productID` INTEGER NOT NULL ,
`warehouseID` INTEGER NOT NULL ,
`expiry` DATE NOT NULL ,
`productID_1` INTEGER NOT NULL ,
PRIMARY KEY (`id`),
KEY `fkIdx_31` (`productID_1`),
CONSTRAINT `FK_31` FOREIGN KEY `fkIdx_31` (`productID_1`) REFERENCES `products` (`productID`)
);
-- ************************************** `roles`
CREATE TABLE `roles`
(
`roleID` INTEGER NOT NULL AUTO_INCREMENT ,
`roleName` VARCHAR(20) NOT NULL ,
`userID` INTEGER NOT NULL ,
PRIMARY KEY (`roleID`),
KEY `fkIdx_96` (`userID`),
CONSTRAINT `FK_96` FOREIGN KEY `fkIdx_96` (`userID`) REFERENCES `users` (`userID`)
);
-- ************************************** `orders`
CREATE TABLE `orders`
(
`orderID` INTEGER NOT NULL AUTO_INCREMENT ,
`orderNum` INTEGER NOT NULL ,
`productID` INTEGER NOT NULL ,
`quantity` INTEGER NOT NULL ,
`userID` INTEGER NOT NULL ,
PRIMARY KEY (`orderID`),
KEY `fkIdx_70` (`productID`),
CONSTRAINT `FK_70` FOREIGN KEY `fkIdx_70` (`productID`) REFERENCES `products` (`productID`),
KEY `fkIdx_100` (`userID`),
CONSTRAINT `FK_100` FOREIGN KEY `fkIdx_100` (`userID`) REFERENCES `users` (`userID`)
);
-- ************************************** `inventory`
CREATE TABLE `inventory`
(
`id` INTEGER NOT NULL AUTO_INCREMENT ,
`expiry` DATE ,
`productID` INTEGER NOT NULL ,
`warehouseID` INTEGER NOT NULL ,
PRIMARY KEY (`id`),
KEY `fkIdx_40` (`productID`),
CONSTRAINT `FK_40` FOREIGN KEY `fkIdx_40` (`productID`) REFERENCES `products` (`productID`),
KEY `fkIdx_62` (`warehouseID`),
CONSTRAINT `FK_62` FOREIGN KEY `fkIdx_62` (`warehouseID`) REFERENCES `warehouses` (`warehouseID`)
);

cannot add foreign key constraint workbench

Executing the following SQL script in the server:
ERROR: Error 1215: Cannot add foreign key constraint
SQL Code:
CREATE TABLE IF NOT EXISTS `telecom`.`jobreq` (
`id` INT NOT NULL,
`jobName` VARCHAR(45) NULL,
`priority` VARCHAR(45) NULL,
`dates` DATE NULL,
`status` VARCHAR(45) NULL,
`user` VARCHAR(45) NULL,
`timestamp` DATE NULL,
`service_id` INT(11) NOT NULL,
`client_id` INT(11) NOT NULL,
PRIMARY KEY (`id`, `service_id`, `client_id`),
INDEX `fk_jobreq_service1_idx` (`service_id` ASC),
INDEX `fk_jobreq_client1_idx` (`client_id` ASC),
CONSTRAINT `fk_jobreq_service1`
FOREIGN KEY (`service_id`)
REFERENCES `telecom`.`service` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_jobreq_client1`
FOREIGN KEY (`client_id`)
REFERENCES `telecom`.`client` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 12 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
I am unable to find out why I am getting this error can someone with a high intelligence please assist me.
So it seems all I had to do was to change the name of the table and it went through im guessing somehow the name was magically somehow still in the system

Error while creating table in PostgreSQL while migrating from MySQL

I am Migrating my database from MySQL to PostgreSQL.While creating table I got an error which I can't resolve.My MySQL Query is like this.
MYSQL Query
CREATE TABLE `configuration` (
`Name` varchar(300) NOT NULL,
`Value` varchar(300) default NULL,
`CType` char(1) default NULL,
`Size` int(11) default NULL,
`CGroup` varchar(50) default NULL,
`RestartReq` char(1) NOT NULL default 'Y',
`Display` char(1) NOT NULL default 'Y',
PRIMARY KEY (`Name`),
KEY `CType` (`CType`),
CONSTRAINT `configuration_ibfk_1` FOREIGN KEY (`CType`) REFERENCES `conftype` (`CType`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_bin`
PostgreSQL Query
CREATE TABLE configuration (
Name varchar(300) PRIMARY KEY,
Value varchar(300) default NULL,
CType char(1) default NULL,
Size integer default NULL,
CGroup varchar(50) default NULL,
RestartReq char(1) NOT NULL default 'Y',
Display char(1) NOT NULL default 'Y',
KEY CType (CType),
CONSTRAINT `configuration_ibfk_1` FOREIGN KEY (CType) REFERENCES conftype (CType)
)
Running File with
psql -h localhost -p 5432 -U postgres -f ps.sql testdb
Error getting
psql:ps.sql:40: ERROR: syntax error at or near "(" at character 287
psql:ps.sql:40: LINE 9: KEY CType ('CType'),
From the MySQL documentation:
KEY is normally a synonym for INDEX.
In PostgreSQL you have to create the index separately from the table:
CREATE TABLE configuration (
name varchar(300) PRIMARY KEY,
value varchar(300),
ctype char(1),
size integer,
cgroup varchar(50),
restartreq boolean NOT NULL DEFAULT true,
display boolean NOT NULL DEFAULT true,
CONSTRAINT configuration_ibfk_1 FOREIGN KEY (ctype) REFERENCES conftype (ctype)
);
CREATE INDEX conf_key ON configuration(ctype);
A few other points:
PostgreSQL identifiers (mainly table and column names) are case-insensitive except when double-quoted. The standard approach is to put identifiers in lower case and keywords in upper case.
Using a varchar(300) as a PRIMARY KEY is usually not a good idea for performance reasons. Consider adding a serial type.
The default value of a column is NULL when nothing is specified, so no need to specify DEFAULT NULL.
PostgreSQL has a boolean data type.

Postgres before insert trigger using sequence from another table

Using Postgres, what I would like to achieve is to be able to have many different instrument types, with corresponding [TYPE].instrument tables, which all have a unique ID in the table, but also reference a unique ID in the instrument.master table. I have the following:
create schema instrument
CREATE TABLE instrument.type (
id smallserial NOT NULL,
name text not null,
code text not null,
CONSTRAINT pk_instrument_type PRIMARY KEY (id)
);
ALTER TABLE instrument.type ADD CONSTRAINT unq_instrument_type_code UNIQUE(code);
ALTER TABLE instrument.type ADD CONSTRAINT unq_instrument_type_name UNIQUE(name);
insert into instrument.type (name, code) values ('futures', 'f');
CREATE TABLE instrument.master (
id serial NOT NULL,
type smallint not null references instrument.type (id),
timestamp timestamp with time zone not null,
CONSTRAINT pk_instrument_master PRIMARY KEY (id)
);
CREATE TABLE futures.definition (
id smallserial NOT NULL,
code text not null,
CONSTRAINT pk_futures_definition PRIMARY KEY (id)
);
ALTER TABLE futures.definition ADD CONSTRAINT unq_futures_definition_code UNIQUE(code);
insert into futures.definition (code) values ('ED');
CREATE TABLE futures.instrument (
id smallserial NOT NULL,
master serial not null references instrument.master (id),
definition smallint not null references futures.definition (id),
month smallint not null,
year smallint not null,
CONSTRAINT pk_futures_instrument PRIMARY KEY (id),
check (month >= 1),
check (month <= 12),
check (year >= 1900)
);
ALTER TABLE futures.instrument ADD CONSTRAINT unq_futures_instrument UNIQUE(definition, month, year);
CREATE OR REPLACE FUNCTION trigger_master_futures()
RETURNS trigger AS
$BODY$
BEGIN
insert into instrument.master (type, timestamp)
select id, current_timestamp from instrument.type where code = 'f';
NEW.master := currval('instrument.master_id_seq');
RETURN NEW;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;
create trigger trg_futures_instrument before insert on futures.instrument
for each row
execute procedure trigger_master_futures();
I then test with:
insert into futures.instrument (definition, month, year)
select id, 3, 2015 from futures.definition where code = 'ED';
Everything works almost as I would like it to. The only issue is that somehow, instrument.master.id ends up being one more than futures.instrument.master. I am not sure what I need to do to achieve the behavior I want, which is that whenever an entry is inserted into futures.instrument, an entry should be inserted into instrument.master, and the id entry of the latter should be inserted into the master entry of the former. I actually think it should have failed since the foreign key relationship is violated somehow.
As it turns out, everything was correct. The issue was that in futures.instrument, the type of the master column is serial, and it should have been int.

pgAdmin - When trying to make a foreign key "referencing" gives no options

I have three tables: ModelingAgency.clients, ModelingAgency.models, ModelingAgency.Bookings. All three tables have a primary key column called id.
The table bookings has two columns that reference clients and models. In pgAdmin when I try to create a foreign key in bookings to either clients or models I get the following screens:
What am I overlooking here? I am new to PostgreSQL (This is my first test project with PostgreSQL -- I've always used MySQL and occasionally SQL Server) so it's probably something obvious (I just don't see it).
EDIT: Here is the DDL, as requested:
-- Table: "ModelingAgency.bookings"
-- DROP TABLE "ModelingAgency.bookings";
CREATE TABLE "ModelingAgency.bookings"
(
id integer NOT NULL DEFAULT nextval('"ModelingAgency.Bookings_id_seq"'::regclass),
"clientId" integer NOT NULL,
"modelId" integer NOT NULL,
"time" timestamp with time zone NOT NULL DEFAULT now(),
"location" character varying(100) NOT NULL DEFAULT 'No Location Selected'::character varying,
CONSTRAINT "bookingId" PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE "ModelingAgency.bookings" OWNER TO "MyBatisTutorial";
-- Table: "ModelingAgency.clients"
-- DROP TABLE "ModelingAgency.clients";
CREATE TABLE "ModelingAgency.clients"
(
id integer NOT NULL DEFAULT nextval('"ModelAgency.clients_id_seq"'::regclass),
"name" character varying(45) NOT NULL,
CONSTRAINT "clientId" PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE "ModelingAgency.clients" OWNER TO "MyBatisTutorial";
-- Table: "ModelingAgency.models"
-- DROP TABLE "ModelingAgency.models";
CREATE TABLE "ModelingAgency.models"
(
id serial NOT NULL,
"name" character varying(45) NOT NULL,
CONSTRAINT "modelId" PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE "ModelingAgency.models" OWNER TO "MyBatisTutorial";
Looking into your posted DDL code I see that your table's names are written in wrong way (that causes your issue with pgAdmin):
"ModelingAgency.bookings"
It should be in format "schema"."tableName":
"ModelingAgency"."bookings"
After that Object browser looks like this (probably you need to create schema first using easily pgAdmin or with CREATE SCHEMA SQL statement):
Here is working DDL code (I omitted some things like OIDS and OWNER TO, but that doesn't matter to your case, BTW OIDS are false on default):
DROP TABLE IF EXISTS "ModelingAgency"."bookings";
CREATE TABLE "ModelingAgency"."bookings"
(
id serial,
"clientId" integer NOT NULL,
"modelId" integer NOT NULL,
"time" timestamp with time zone NOT NULL DEFAULT now(),
"location" character varying(100) NOT NULL
DEFAULT 'No Location Selected'::character varying,
CONSTRAINT "bookingId" PRIMARY KEY (id)
);
DROP TABLE IF EXISTS "ModelingAgency"."clients";
CREATE TABLE "ModelingAgency"."clients"
(
id serial,
"name" character varying(45) NOT NULL,
CONSTRAINT "clientId" PRIMARY KEY (id)
);
DROP TABLE IF EXISTS "ModelingAgency"."models";
CREATE TABLE "ModelingAgency"."models"
(
id serial NOT NULL,
"name" character varying(45) NOT NULL,
CONSTRAINT "modelId" PRIMARY KEY (id)
)