Conditional constraint based on value in another table? - sql

With Postgres, I'm not sure how to set this up. I have two tables (pseudocode):
Table A
ID int
Unique bool
Table B
TableA_ID int
UserID int
What I want is if Unique in a row in Table A is true, to set a constraint on all rows in Table B that reference that TableA_ID row, the constraint being that (TableA_ID, UserID) are unique. But only for those rows that reference that particular Table A row.
Edit: This is for a database system. That is, each row in Table A describes a table of data (name, description, what the columns are, etc), and each row in Table B contains the values for a row in a table from Table A, along with some meta data like who created the row. We want to have the ability to designate some tables such that each user can only add one row to them, while other tables we don't care.

Related

How to setup table A in such way, that a only one row from table B with a certain value can have foreign key relation to a row from table A?

We have table A. Table B has foreign key to A. And now, if there is already a row in table B with attribute surname="Jordan" that has foreign key relation to a particular row in A , it will be impossible to create another row in table B with the same surname that will have relation with the same row in A.
How to solve this problem.
Create a unique index on Table B's foreign key column.
Note that this suggests Table B should probably be folded into Table A.

SQL how to delete rows across tables with same ID

In my tables for SQL, I have many tables each with an ID column I wish to keep incrementing, along with a main name table with an ID which will be the same value as the corresponding IDs in other tables for the relevant information. I wish to be able to make it so that, when I delete a row from the name table with a certain ID, all values across other tables with the same ID get deleted also. The issue appears to be that, if i create foreign key constraints from the name ID on the name table to the other IDs across other tables, it means these tables are no longer allowed to automatically increment.

INSERTING a single field into a table, referenced from another table

I need to insert a field in a that references an id field in another table.
The id field it is to going is next to the field 'test' (column - codedescription, table typecategory) and coming from an id field next to the word 'assessment' (column categorydescription, table typecategory)
INSERT INTO codetype
(typecategoryid)
Where codedescription='test'
SELECT id FROM typecategory WHERE categorydescription='Assessment Types'
There are plenty of examples of inserting entire columns but nobody has written how to insert a single field from another table.
table - codetype
id bigserial primary key
codedescription varchar
typecategoryid bigint foreign key to typecatogory on the ID column
Table - typecategory
ID big serial primary key
categorydescription varchar
If the column already exists and there are are already records in the rest of the columns in the table, then you need an UPDATE statement, not an INSERT.
Looks like this post might help you: Update a column of a table with a column of another table in PostgreSQL
maybe
UPDATE codetype c
SET c.typecategoryid = t.id
FROM typecategory t
WHERE c.codedescription = 'test' and t.categorydescription='Assessment Types'

SQL Server , inserting existing rows into the same table

I have a table say Table A
Col A is the primary key (identity).
Col b is a foreign key from some other table.
Now, for example I want to select the rows where colB = 2 , and insert the rows back into the table again but with a different value of col B say 5 . This can be done easily. Next once the rows are inserted again, I need to find the relation between the copies, especially.
Say new rows are inserted with primary key 6,7,8. Now I want to check which parent row (row with primary key value 1/2/3) has the same data as row with primary key 6. Similarly, I need to find relative for the row with primary key 7, 8. One way is to do the insert one row at a time so that we can say that row with primary key 1 is the copy of row with primary key 6. Another way to do is to join all the columns. I am trying to avoid all these to see if there is any easy solution possible.
Use SCOPE_IDENTITY() to get last primary key inserted value on your table. If another process could be modifying the table then do your update & query in one transaction.

Copying a 1-to-many relationship between two tables to another two tables (SQL 2005)

I have two tables with a parent-child relationship. I would like to copy some of their records to two other tables, also with a parent-child relationship, but with a slightly different table structure.
There is a foreign key involved with both sets of tables, an integer column. All tables have the identity increment on for their primary key columns.
If I do a SELECT INTO from the source parent table to the destination parent table, the primary key values for the destination records will be different from the source records, and the parent-child relationship will be lost.
Does anyone know how I can preserve this relationship during the copy, given that I'll have new primary key values in the new parent table? I'd prefer not to set the identity increment off for the new tables during this copy procedure, because there's no guarantee the primary key values in the source table won't already be in the destination.
Hope my description makes sense, and thanks for your opinions. Let me know if I can clarify further.
When I have done this, I did so by preserving the old ID after the insert into the new table.
Some people add a temporary column with the old ID, but I know of a better way.
You can use the OUTPUT clause to insert the inserted records into a temp table (with the new and the old IDs as a mapping table. Then join to that when inserting the child records.
Something like this (crude)
DECLARE #MyTableVar TABLE (
NewID INT,
OldID INT
)
INSERT NewParentTable
OUTPUT
INSERTED.ID,
old.ID
INTO #MyTableVar
SELECT *
FROM OldParentTable old
INSERT NewChildTable
SELECT
old.ID,
t.NewID,
old.name
FROM OldChildTable old
INNER JOIN #MyTableVar t ON t.OldID = old.ParentID
You'd have to maintain the original ID in a separate column in the table with the corresponding parentID.
So in your destination table, you'd need to add an parentID_orig column (not auto-number) to retain the link.