UNIQUE constraint failed SQL - sql

I want to create two Tables, but it does not work because of "UNIQUE constraint failed".
Can someone explain to me what is wrong with my querees?
CREATE TABLE A (
ka INT PRIMARY KEY,
a2 VARCHAR(1)
);
INSERT INTO A VALUES (1,'s');
INSERT INTO A VALUES (2,'s');
INSERT INTO A VALUES (3,'s');
INSERT INTO A VALUES (4,'m');
INSERT INTO A VALUES (5,'m');
INSERT INTO A VALUES (6,'b');
INSERT INTO A VALUES (7,'b');
INSERT INTO A VALUES (8,'b');
INSERT INTO A VALUES (9,'b');
CREATE TABLE B (
a2 VARCHAR(1),
b2 INT,
PRIMARY KEY (a2),
FOREIGN KEY (a2) REFERENCES A(a2)
);
INSERT INTO B VALUES ('s',12);
INSERT INTO B VALUES ('s',23);
INSERT INTO B VALUES ('s',34);
INSERT INTO B VALUES ('m',45);
INSERT INTO B VALUES ('m',56);
INSERT INTO B VALUES ('b',67);
INSERT INTO B VALUES ('b',78);
INSERT INTO B VALUES ('b',89);
INSERT INTO B VALUES ('b',90);

(Edited: there is more than one issue here.)
A primary key for table B must be unique. In your case, it needs to be something other than a2. If you need a primary key (usually you do just as a part of good design), you likely need a separate dedicated column.
Something like:
CREATE TABLE B (
kb INT,
a2 VARCHAR(1),
b2 INT,
PRIMARY KEY (kb),
--FOREIGN KEY (a2) REFERENCES A(a2) -- This is also an issue
);
INSERT INTO B VALUES (101, 's',12);
INSERT INTO B VALUES (102, 's',23);
INSERT INTO B VALUES (103, 's',34);
INSERT INTO B VALUES (104, 'm',45);
INSERT INTO B VALUES (105, 'm',56);
INSERT INTO B VALUES (106, 'b',67);
INSERT INTO B VALUES (107, 'b',78);
INSERT INTO B VALUES (108, 'b',89);
INSERT INTO B VALUES (109, 'b',90);
EDIT:
The second issue is that your foreign key from table B is referencing a non-unique column in table A. Foreign keys define a many-to-one relationship, but your data indicates a potentially many-to-many relationship. The first three "s" rows in table B each match all three "s" rows in table A. While you can define a JOIN in a query that matches ON B.a2 = A.a2, you can't use a foreign key to define this relationship.

Related

SQL table with primary key over two columns and unique values in both columns

How to create a table in SQL with the following attributes?
The table has two columns A and B.
The primary key of the table is (A, B).
All values in A are unique. Pseudo code: (Count(A) == COUNT(SELECT DISTINCT A)).
All values in B are also unique.
CREATE TABLE IF NOT EXISTS myTable(
A VARCHAR(32) PRIMARY KEY NOT NULL, -- A HAS DISTINCT VALUES
B VARCHAR(32) NOT NULL -- B HAS DISTINCT VALUES
);
INSERT INTO myTable VALUES ('A1', 'B1') --> Add value
INSERT INTO myTable VALUES ('A1', 'B2') --> Do not add value
INSERT INTO myTable VALUES ('A2', 'B2') --> Add value
INSERT INTO myTable VALUES ('A3', 'B3') --> Add value
INSERT INTO myTable VALUES ('A4', 'B3') --> Do not add value
INSERT INTO myTable VALUES ('A4', 'B4') --> Add value
INSERT INTO myTable VALUES ('A5', 'B6') --> Add value
To define a compound PRIMARY KEY:
CREATE TABLE myTable
(
A VARCHAR(32) NOT NULL,
B VARCHAR(32) NOT NULL,
CONSTRAINT PK_AB primary key (A,B),
CONSTRAINT UQ_A UNIQUE(A),
CONSTRAINT UQ_B UNIQUE(B)
);
Please note: a table with just 2 columns with both columns in the primary key smells funny.

SQL doesn't allow me to insert data

creating table and inserting data into a table and now it giving me an error
SQL Error: ORA-02291: integrity constraint (S21403051.SYS_C007300)
violated - parent key not found 02291. 00000 - "integrity
constraint (%s.%s) violated - parent key not found" *Cause:
CREATE TABLE CUSTOMER(
CUSTOMER_ID VARCHAR(10) PRIMARY KEY,
FIRST_NAME VARCHAR(10),
SURNAME VARCHAR(15),
CUSTOMER_TEL VARCHAR(12),
CUSTOMER_EMAIL VARCHAR(30)
)
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_1', 'CUST_102', 'EMP_51');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_3','CUST_101','EMP_51');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_3','CUST_101','EMP_53');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_5','CUST_103','EMP_54');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_5','CUST_107','EMP_54');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_1', 'CUST_106','EMP_55');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_1','CUST_108','EMP_55');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_5','CUST_104','EMP_51');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_3','CUST_109','EMP_51');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_2','CUST_1010','EMP_52');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_2','CUST_1010','EMP_55');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_5','CUST_101','EMP_51');
INSERT INTO CUSTOMER_CRUISES VALUES ( 'CRUISE_5','CUST_103','EMP_51');
When you have defined 6 columns CUSTOMER_ID , FIRST_NAME , SURNAME ,
CUSTOMER_TEL,CUSTOMER_EMAIL and when you try to insert values, it takes in order of table definition. Instead you can try this way
Example:
INSERT INTO CUSTOMER_CRUISES
(column1, clumn2,column3)
values ('xx','xy','yz')
Yet, you can not insert duplicate value into first column as primary key is defined on it. And, it cant be null.
Hope this helps
Cause: A foreign key value has no matching primary key value.
This problem arises when you attempt to insert a record containing the Customer_ID column into the child table (CUSTOMER_CRUISES) and that this Customer_ID is not present in the parent table (CUSTOMER). When the Customer_ID (Foreign key) in CUSTOMER_CRUISES table does not get to reference the Customer_ID (primary key) in CUSTOMER table, an error is raised.
One workaround is to insert and make sure that the value is present in the CUSTOMER table first before inserting the values into the CUSTOMER_CRUISES table.

PSQL unique constraint on two columns

I have a table which I use to link two more tables together in a 1 to many relationship.
TableA
id
name
TableB
id
name
LinkTable
TableA_id
TableB_id
Basically, one of TableA can have many of TableB. Very simple. The problem I have now is to create a constraint that follows the rules of this relation ship as such:
LinkTable
TableA_id TableB_id
1 1
1 2
1 3
2 1
2 2
2 3
I want to create a unique constraint which combines both the columns together as the unique value. So in the link table above, with this new constraint, I can
INSERT INTO LinkTable (TableA_id, TableB_id) VALUES (1, 4);
INSERT INTO LinkTable (TableA_id, TableB_id) VALUES (1, 5);
INSERT INTO LinkTable (TableA_id, TableB_id) VALUES (2, 6);
INSERT INTO LinkTable (TableA_id, TableB_id) VALUES (3, 1);
With out any problems
And if I try to insert:
INSERT INTO LinkTable (TableA_id, TableB_id) VALUES (1, 1);
INSERT INTO LinkTable (TableA_id, TableB_id) VALUES (1, 3);
The constraint will fire because there is already a row with 1,1 and 1,3. How would I create a postgres constraint to do this? If i set a unique constraint to both rows, then I cannot have more then one TableA_id the same and more then one TableB_id the same.
What is the solution?
have you tried setting both columns as unique?
ALTER TABLE LinkTable
ADD CONSTRAINT LinkTable_Unique UNIQUE (TableA_id, TableB_id);
create table LinkTable (
TableA_id integer REFERENCES TableA(id),
TableB_Id integer REFERENCES TableB(id),
UNIQUE(tableA_id, tableB_id)
);

How to avoid bad data entries in table through proper Key relationship?

I have 3 tables. widgets, widget types, widget type ID. I have created direct relationship between them. How can I avoid bad data going into Widget tables based on Widget_type and Widget_sub_Type. Please see my code. There is just one small thing missing.
Create table widgets (
Widget_ID int not null primary key,
Widget_type_ID int not null,
Widget_Sub_type_ID int,
Widget_Name varchar (50)
)
Create table Widget_Type (
Widget_Type_ID int not null primary key,
)
Create table Widget_Sub_type (
Widget_Sub_Type_ID int not null primary key,
Widget_Type_ID int not null
)
---adding foregin key constraints
Alter table widgets
ADD constraint FK_Widget_Type
FOREIGN KEY (Widget_type_ID)
References Widget_Type (Widget_type_ID)
Alter table widgets
ADD constraint FK_Widget_Sub_Type
FOREIGN KEY (Widget_Sub_type_ID)
References Widget_SUB_Type (Widget_SUB_type_ID)
Alter table widget_Sub_Type
ADD Constraint FK_Widget_Type_Alter
Foreign key (widget_Type_ID)
References Widget_Type (Widget_Type_ID)
---- insert values
insert Widget_Type values (1)
insert Widget_Type values (5)
insert Widget_Sub_type values (3,1)
insert Widget_Sub_type values (4,1)
insert Widget_Sub_type values (7,5)
insert Widget_Sub_type values (9,5)
-- This will error out which is correct
insert Widget_Sub_type values (5,6)
select * from Widget_Sub_type
select * from Widget_type
--Good
insert widgets (Widget_ID,Widget_Name, Widget_type_ID, Widget_Sub_type_ID)
values (1, 'TOY', 1, 3)
select * from widgets
--Good
insert widgets (Widget_ID,Widget_Name, Widget_type_ID, Widget_Sub_type_ID)
values (2, 'BatMan', 5, 7)
-- How to prevenet this, 3 is not sub_type_id of type_ID 5. This is bad data, It should not be inserted.
insert widgets (Widget_ID,Widget_Name, Widget_type_ID, Widget_Sub_type_ID)
values (3, 'Should Not', 5, 3)

Duplicating records efficiently in tsql

I've a scenario where I have a parent table which has '1 to many' relationships with two or three tables. These child tables again have '1 to many' relationships with more tables and so on. This goes up to 5 to 6 levels of hierarchy.
Now, based on single primary key value of the parent table, I want to duplicate all information related to it in database. I wrote a stored procedure which uses cursors and inserts child rows one by one and sets new foreign key values with each insert. But it is consuming some time because number of records in child tables is high.
Is there any other efficient way to do this?
In SQL Server 2008:
CREATE TABLE t_parent (id INT NOT NULL PRIMARY KEY IDENTITY, value VARCHAR(100))
CREATE TABLE t_child (id INT NOT NULL PRIMARY KEY IDENTITY, parent INT NOT NULL, value VARCHAR(100))
CREATE TABLE t_grandchild (id INT NOT NULL PRIMARY KEY IDENTITY, child INT NOT NULL, value VARCHAR(100))
INSERT
INTO t_parent (value)
VALUES ('Parent 1')
INSERT
INTO t_parent (value)
VALUES ('Parent 2')
INSERT
INTO t_child (parent, value)
VALUES (1, 'Child 2')
INSERT
INTO t_child (parent, value)
VALUES (2, 'Child 2')
INSERT
INTO t_grandchild (child, value)
VALUES (1, 'Grandchild 1')
INSERT
INTO t_grandchild (child, value)
VALUES (1, 'Grandchild 2')
INSERT
INTO t_grandchild (child, value)
VALUES (2, 'Grandchild 3')
DECLARE #tt TABLE (oid INT, nid INT)
MERGE
INTO t_parent
USING (
SELECT id, value
FROM t_parent
) p
ON 1 = 0
WHEN NOT MATCHED THEN
INSERT (value)
VALUES (value)
OUTPUT p.id, INSERTED.id
INTO #tt;
MERGE
INTO t_child
USING (
SELECT c.id, p.nid, c.value
FROM #tt p
JOIN t_child c
ON c.parent = p.oid
) c
ON 1 = 0
WHEN NOT MATCHED THEN
INSERT (parent, value)
VALUES (nid, value)
OUTPUT c.id, INSERTED.id
INTO #tt;
INSERT
INTO t_grandchild (child, value)
SELECT c.nid, gc.value
FROM #tt c
JOIN t_grandchild gc
ON gc.child = c.oid
In earlier versions of SQL Server, you will have to do a SELECT followed by an INSERT to find out the new values of the PRIMARY KEY.
You'll have to insert one table at a time, but you can do it by inserting sets instead of rows if you allow the FK values in the new parent's child tables to be the same as the FK values of the original parent.
Say you have a view of your parent table and in your sp you limit it to the row to copy from (pk=1, say).
Then insert that row into the parent table substituting PK=2 for the PK val.
Now use a second view of one of the child tables. In your sp, limit the set of rows to those with PK=1. Again, insert all those rows into that same child table substituting PK=2 for the PK field val.