This is an extension of Insert values into table B based on column from table A?
From the above question, let's say in both the User_Permissions and Users table there's also 2 more columns recorded for audit purposes: a version column and a transaction_version column. When inserting the new row (which is based on a row from the Users table) into the User_Permissions table I need to take the value of the 2 columns in the Users table, increment it by 1 and then insert it into the User_Permissions table.
Is there an easy SQL query to do this? I suspect it'd have to do with another inline select but am unsure of the syntax.
You could use Triggers after insert to perform the needed updates
Related
I will try to be short and concise, I have this table which contains rows of data.
I also have 3 separate tables, Surgery, TypeOfSurgery and Procedure, each filled with additional data, and each should contain the Id (foreign key) to the previous table.
For example TypeOfSurgery has a FK column SurgeryId, Procedure has FK column TypeOfSurgeryId etc, I need to update the existing entries in these tables!
And I also have 3 dropdowns, each connected to the previous one.
EXPECTED OUTPUT:
If I choose Abdominal Surgery the following dropdown should be populated with Appendectomy and Caesarian Section, further if I choose Appendectomy the next dropdown should be populated with procedure A, and if I choose Caesarian section the next dropdown should contain procedure B and Procedure C etc...
My question is how do I give the corresponding IDs to each row in each table with one query, so the dropdowns will work as I described above.
Thanks!
It sounds like you're asking how to decompose this denormalised data into three different tables.
This is how I would do it.
(I wouldn't normally answer a question where the post doesn't contain DDL)
First ensure that all three target tables have an identity field defined against the PK so that keys are autogenerated.
Populate Surgery. SurgeryID in this table must be IDENTITY
INSERT INTO Surgery (SurgeryName)
SELECT DISTINCT Surgery FROM YourTable
Populate TypeOfSurgery. TypeOfSurgeryId must be IDENTITY
INSERT INTO TypeOfSurgery (SurgeryID, TypeOfSurgeryName)
SELECT DISTINCT S.SurgeryID, SRC.TypeOfSurgery
FROM YourTable SRC
INNER JOIN Surgery S
ON S.SurgeryName = SRC.Surgery
Populate Procedure. ProcedureId must be IDENTITY
INSERT INTO Procedure (TypeOfSurgeryId, ProcedureName)
SELECT DISTINCT S.TypeOfSurgeryId, SRC.Procedure
FROM YourTable SRC
INNER JOIN TypeOfSurgery S
ON S.TypeOfSurgeryName = SRC.TypeOfSurgery
I have this fairly large DB. It contains lots of column. One of the will have a value that I need to select, but the DB has several of that value. How can I insert into a column in the row thats the newest in the DB, with a matching column.
Without knowing the ins and outs of your database, I think you would likely want to select the largest id you have in the auto incrementing row. For instance:
SELECT MAX(UNIQUE_ID) FROM TABLE WHERE MATCHING_COLUMN = MATCHING_VALUE
From there you can take your unique ID and insert into that row.
I have three tables:
Table A has columns name, id, nationality
Table B has a column name
Table C has a column id
I was wondering if it is possible to extract from Table A and insert its name column into Table B and id column into Table C in one single SQL query? Not in two separate queries.
I know it is possible in Oracle.
I am using Teradata, which supports all SQL queries.
It is not possible to do in a single query. One table at a time only. Use a Transaction or a Stored statement to query the data and then two more queries to insert the data in each table. This does save you making the query for both inserts but you cant do an INSERT on two tables.
I need to INSERT a row in table_A depending on the information in a row in table_B.
Is it possible to do this in an isolated way where the SELECT retrieval of the row from table B is locked until either the new row is INSERTed into table_A or the INSERT is skipped due to the information in table_B's row?
It's really not clear what you are trying to say , i think your problem is solved by using a trigger .
check this site for know more about trigger
http://www.codeproject.com/Articles/25600/Triggers-SQL-Server
You can do this:
INSERT INTO A (columns) select columns from table B where condition;
Columns retrieved from the query must match the queries defined in the table A.
PostgreSQL supports MVCC, custom locking can be done but it is not recomended.
How do delete duplicate records in a DB2 table? I want to be left with a single record for each group of dupes.
Create another table "no_dups" that has exactly the same columns as the table you want to eliminate the duplicates from. (You may want to add an identity column, just to make it easier to identify individual rows).
Insert into "no_dups", select distinct column1, column2...columnN from the original table. The "select distinct" should only bring back one row for every duplicate in the original table. If it doesn't you may have to alter the list of columns or have a closer look at your data, it may look like duplicate data but actually is not.
When step 2 is done, you will have your original table, and "no_dups" will have all the rows without duplicates. At this point you can do any number of things - drop and rename tables, or delete all from the original and insert into the original, select * from no_dups.
If you're running into problems identifying duplicates, and you've added an identity column to "no_dups," you should be able to delete rows one by one using the identity column value.