table creation failed because a foreign key constraint was not correctly formed
the first create table my_seeking, works fine, when I try to run the create contactstoseeking I get the following err msg
Error Code: 1005. Can't create table 'db_ferguson.contactstoseeking'
(errno: 150)
CREATE TABLE IF NOT EXISTS `my_seeking` (
`id` INT NOT NULL ,
`seeking` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB
;
CREATE TABLE IF NOT EXISTS `contactsTOseeking` (
`id` INT NOT NULL ,
`seek` VARCHAR(45) NOT NULL ,
UNIQUE INDEX `id_UNIQUE` (`id` ASC) ,
UNIQUE INDEX `seek_UNIQUE` (`seek` ASC) ,
CONSTRAINT `fk_contactsTOseeking_my_seeking1`
FOREIGN KEY (`id` )
REFERENCES `mydb`.`my_seeking` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
A search of the web shows that err 150 is tied to my fk constraint.
Cannot create table. If the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed. If the error message refers to error –1, table creation probably failed because the table includes a column name that matched the name of an internal InnoDB table."
this code was generated BY MySQL, and this is my first attempt at foreign keys. I tried coding it manually and had issues so I thought that I would let workbench do it. WHAT EVER the problem is I cant see it or it is beyond my current skill set
Did you try removing the 'mydb' text in the reference? It indicates a table in 'mydb' database.
Related
I have created two tables in Snowflake.
create or replace TRANSIENT TABLE TESTPARENT (
COL1 NUMBER(38,0) NOT NULL,
COL2 VARCHAR(16777216) NOT NULL,
COL3 VARCHAR(16777216) NOT NULL,
constraint UNIQ_COL3 unique (COL3)
);
create or replace TRANSIENT TABLE TESTCHILD3 (
COL_A NUMBER(38,0) NOT NULL,
COL_B NUMBER(38,0) NOT NULL,
ABCDEF VARCHAR(16777216) NOT NULL,
constraint FKEY_1 foreign key (COL_A, COL_B) references TEST_DB.PUBLIC.TESTPARENT1(COL1,COL2),
constraint FKEY_2 foreign key (ABCDEF) references TEST_DB.PUBLIC.TESTPARENT(COL3)
);
Now I want to execute a query and see the names of columns that are involved in FKEY_2 FOREIGN KEY
in Table TESTCHILD3, but it seems like there are no DB Table/View that keeps this information. I can find out the column names for UNIQUE KEY & PRIMARY KEY but there is nothing for FOREIGN KEYS.
EDIT
I have already tried INFORMATION_SCHEMA.TABLE_CONSTRAINTS, along with INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS and all the other system tables. No luck. Only DESC TABLE is giving me some info related to CONSTRAINTS and COLUMNS but that also has FOREIGN KEY CONSTRAINTS information missing.
SHOW IMPORTED KEYS IN TABLE <fk_table_name>;
Updated answer:
I was checking on something unrelated and noticed a very efficient way to list all primary and foreign keys:
show exported keys in account; -- Foreign keys
show primary keys in account;
When you limit the call to a table, it appears you have to request the foreign keys that point to the parent table:
show exported keys in table "DB_NAME"."SCHEMA_NAME"."PARENT_TABLE";
You can check the documentation for how to limit the show command to a specific database or schema, but this returns rich information in a table very quickly.
maybe you can try to query this view: INFORMATION_SCHEMA.TABLE_CONSTRAINTS
Note: TABLE_CONSTRAINTS only displays objects for which the current role for the session has been granted access privileges.
For more see: https://docs.snowflake.net/manuals/sql-reference/info-schema/table_constraints.html
I get this when I try to connect my DB when I run the following
psql newsdata.sql
here is the whole output
vagrant#vagrant:/vagrant$ psql -d news -f newsdata.sql
SET
SET
SET
SET
SET
SET
SET
SET
SET
psql:newsdata.sql:31: ERROR: relation "articles" already exists
ALTER TABLE
psql:newsdata.sql:45: ERROR: relation "articles_id_seq" already exists
ALTER TABLE
ALTER SEQUENCE
psql:newsdata.sql:65: ERROR: relation "authors" already exists
ALTER TABLE
psql:newsdata.sql:79: ERROR: relation "authors_id_seq" already exists
ALTER TABLE
ALTER SEQUENCE
psql:newsdata.sql:102: ERROR: relation "log" already exists
ALTER TABLE
psql:newsdata.sql:116: ERROR: relation "log_id_seq" already exists
ALTER TABLE
ALTER SEQUENCE
ALTER TABLE
ALTER TABLE
ALTER TABLE
psql:newsdata.sql:162: ERROR: duplicate key value violates unique constraint "articles_pkey"
DETAIL: Key (id)=(23) already exists.
CONTEXT: COPY articles, line 1
setval
--------
30
(1 row)
psql:newsdata.sql:181: ERROR: duplicate key value violates unique constraint "authors_pkey"
DETAIL: Key (id)=(1) already exists.
CONTEXT: COPY authors, line 1
setval
--------
4
(1 row)
psql:newsdata.sql:1677931: ERROR: duplicate key value violates unique constraint "log_pkey"
DETAIL: Key (id)=(1678923) already exists.
CONTEXT: COPY log, line 1
setval
---------
3356657
(1 row)
psql:newsdata.sql:1677946: ERROR: multiple primary keys for table "articles" are not allowed
psql:newsdata.sql:1677954: ERROR: relation "articles_slug_key" already exists
psql:newsdata.sql:1677962: ERROR: multiple primary keys for table "authors" are not allowed
psql:newsdata.sql:1677970: ERROR: multiple primary keys for table "log" are not allowed
psql:newsdata.sql:1677978: ERROR: constraint "articles_author_fkey" for relation "articles" already exists
I just don't understand what's dublicated key so I can fix it, I'm begginner could you provide a detailed answer for this? even if it's just a link to check, it would help a lot.
DETAIL: Key (id)=(23) already exists.
DETAIL: Key (id)=(1) already exists.
Script attempted to insert id=23 or id=1 but theses values are already in table.
So unique constraint violated, and each tuple have not unique id.
solved.
The problem was because I ran this command before so now I tried psql -d news and it worked.
the output showed a table for relations
I keep having a cryptic "Syntax error" in this statement for an Informix database.
CREATE TABLE Historial
(
id_evento SERIAL PRIMARY KEY CONSTRAINT Historial_claves_primarias,
foranea_CI_Persona INT REFERENCES Personas (CI) CONSTRAINT Historial_fk_Personas_CI,
IP varchar(20) NOT NULL CONSTRAINT ip_vacia,
query lvarchar(1000) NOT NULL CONSTRAINT Historial_query_vacia,
fecha_hora DATETIME NOT NULL CONSTRAINT fecha_historial_vacio
);
Can someone help me and point out what it is? I seriously cant see it.
I have just found out that im supposed to give the precision of the measure as stated below, but with a cryptic error like that, what do you expect.
drop table if exists Historial;
CREATE TABLE Historial
(
id_evento SERIAL PRIMARY KEY CONSTRAINT Historial_claves_primarias,
foranea_CI_Persona INT REFERENCES Personas (CI) CONSTRAINT Historial_fk_Personas_CI,
IP varchar(20) NOT NULL CONSTRAINT ip_vacia,
query lvarchar(1000) NOT NULL CONSTRAINT Historial_query_vacia,
fecha_hora datetime year to minute not null constraint Historial_fecha_vacia
);
I am using Entity Framework 6.1.3 and a database first approach.
It is a small database with a many to many relationship between Tags and BoxedItems, a table named ItemsTags holds the relationship.
I get an exception when using the scaffolded code to delete a BoxedItem:
db.BoxedItems.Remove(boxedItem);
db.SaveChanges();
SqlException: The DELETE statement conflicted with the REFERENCE
constraint "FK_ItemsTags_Items". The conflict occurred in database
"TimeBox", table "dbo.ItemsTags", column 'IdItem'.
The relationship table code is bellow. The PK for BoxedItem needs to be a Guid and for Tags is a INT IDENTITY (1, 1).
CREATE TABLE [dbo].[ItemsTags] (
[IdItem] UNIQUEIDENTIFIER NOT NULL,
[IdTag] INT NOT NULL,
CONSTRAINT [PK_ItemsTags] PRIMARY KEY CLUSTERED ([IdItem] ASC, [IdTag] ASC),
CONSTRAINT [FK_ItemsTags_Tags] FOREIGN KEY ([IdTag]) REFERENCES [dbo].[Tags] ([Id]),
CONSTRAINT [FK_ItemsTags_Items] FOREIGN KEY ([IdItem]) REFERENCES [dbo].[BoxedItems] ([Id])
);
Would the EF auto generated code work out of the box if my BoxedItem PK was INT IDENTITY (1, 1)? EF seems to like it more for auto generated code.
Is there a smarter way to delete the BoxedItem, other than a custom SQL instruction?
Continuing to the post of reading-from-XML-and-storing-the-values-in-database-using-grails
am facing an another problem in it. As the employee id is related to other tables as foreign key how am suppose to insert the data into the database without conflict what i want to add in grails code to avoid the below displayed error.
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:Cannot
add or update a child rowL a foreign key constraint fails.. ( CONSTRAINT 'EMPLOYEE_ID_HEADER'
FOREIGN KEY ('EMPLOYEE_ID') REFERENCES 'employee_header'('employee_id'))
here employee_id is a column and employee_header is a separate table that contains employee_id as a foreign key.
Finally I got the answer, when you are inserting the values in database the related foreign key tables will get affects initially. So to handle this situation we have to insert the foreign key related table at first then the table we need to add exactly.
For the reference, at first I had given the code like this
sql.executeInsert("insert into order_item (order_id,product_id,
order_item_seq_id) values (${order_id},${product_id},${order_item_seq_id})")
sql.executeInsert("insert into product(product_id) values(${product_id})")
This first inserts the order_item table then the foreignkey constraint table product, so the data did not inserted. So the correct code is
sql.executeInsert("insert into order_header(order_id) values(${order_id})")
sql.executeInsert("insert into product(product_id) values(${product_id})")
sql.executeInsert("insert into order_item (order_id,product_id,
order_item_seq_id) values (${order_id},${product_id},${order_item_seq_id})")
Now this inserts the data successfully without errors.