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
);
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 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?
I know that new feature of core data is unique constraints so I decide to test it. I created simple schema and when I add constraint for one field (firstName for example) - all works. If I add constraint for another field (lastName) - core data creates two different constraints for each field individually (I tried add two fields comma separated in one line and each field in separate line - no difference). But I want unique constraint for combination firstName + lastName, is it possible?
Thank you for reply!
It is possible to set uniqueness constraints for a combination of attributes. You were on the right path putting both attributes on the same line in the constraints:
You may have found this had no effect due to a problem with Xcode: the constraints are not actually updated in the model unless you modify some other aspect of the model at the same time (eg. change an attribute type and then change it back).
If you look at the SQL being generated, the table is created with a constraint:
CREATE TABLE ZSTOREOBJECT ( Z_PK INTEGER PRIMARY KEY, Z_ENT INTEGER, Z_OPT INTEGER, ...., CONSTRAINT ZLASTNAME_ZFIRSTNAME UNIQUE (ZLASTNAME, ZFIRSTNAME))
and when you insert new insert new values which fail this constraint (when the context is saved):
CoreData: sql: COMMIT
CoreData: sql: BEGIN EXCLUSIVE
CoreData: sql: INSERT INTO ZSTOREOBJECT(Z_PK, Z_ENT, Z_OPT) VALUES(?, ?, ?)
CoreData: sql: UPDATE ZSTOREOBJECT SET ZLASTNAME = ?, ZFIRSTNAME = ? WHERE Z_PK = ?
CoreData: sql: ROLLBACK
and the error returned is:
Error Domain=NSCocoaErrorDomain Code=133021 "(null)" UserInfo={conflictList=(
"NSConstraintConflict (0x7fbd18d33c10) for constraint (\n lastName,\n firstName\n): ....
(This works in Xcode 7.2.1, with the iOS9.2 simulator; I haven't checked prior versions).
It seems that Xcode's typical behavior of persisting changes to disk on Run doesn't apply when setting constraints in the model editor. (Xcode 7.3, in my case.) 😞
Solution? Pretend it's 1990 all over again and hit ⌘-S after you change the constraints.
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.
I use sqlite(3.7.4) in iphone.
I create a table like:
create table A (UserName varchar (50) primary key, Num integer);
Then I insert the record below twice:
('abc',1);
Normally there should be only one record in DB.
However I found in the DB
(abc,1);
( ,1);
I'm confused that as UserName is primary key and why there are two records!
I don't know what's the problem.
Can anyone help me?
thank you.
Why you want use username as primary key. Primary key should unique identify record. What is why when you inserted twice in second time you have primary key constraint violation
create table A (UserName varchar (50) primary key, Num integer, unique (UserName));