How to change the parent value, affect the children value between 2 table in SQL - sql

For example, if there are two tables:
Table 1: Company
which have a attribute : status
Table 2 : Employee
which have a attribute : status
If table 1 attribute change to 1/0, the table 2 have to follow, however, if table 2 change to 1/0, there should be no action for table 1. How to construct that?

You can create database trigger on update of table 1.
If you use MySQL you can read about it here: http://dev.mysql.com/doc/refman/5.0/en/trigger-syntax.html

Related

SQL Join Problem Table two has more as one Record for ID

I have a SQL query problem.
i have two tables.
Table One UINT ID and same Fields
Table Two have Records with ID from the first Table. but more than one record for the ID.
Table Two:
ID=1 StringField1= "Hello" Stringfield2="World"
ID=1 StringField1= "Auto" Stringfield2="Street"
Is there a way to construct a query in such a way that I get a record of the form
1,Hello,World,Auto,Street,....etc.
Lot of Thanks.
Winfried
Have you a solution for this Problem?
Hello.
In Table 1 as the main Table is the field C_URI. This Field is the Key for the Table 2 , Field FK_Workitem. In Table 1 is one Record for the Key.
In Table 2 are the Costumer Fields with teh Fieldname and Feild value. in our Example:
Field 1 -> Name variationType value -> mandfeature
Field2 -> Name swe1_coverage value -> started
I now want to make a query in which I only have one record
C_URI, variationType value, swi_coverage value.
What must i do?
Select Statement
Output

Oracle PL/SQL Query, Adding new column through looping

I have a table in excel which is called the test table:
I need to create an additional column called level in the table through the following steps.
Whenever 'parent' column is equal to 'EEP', Check the values in the location column. In this table, the location column has only A under the filter of EEP for parent column. Now Assign value of 1 in the level column
Now check value A under parent column. Values under location are B,N,S. Assign 2 under level column
Now check B,N,S under parent column. Values under location are C,D,Q,U,V. Assign 3 under level column
Now check C,D,Q,U,V under parent column. values corresponding to location are R, M. Assign 4 under level column.
My final table should look something like this.
I need to write a query in Oracle SQL to do the same for all rows in the parent column adding one in the level column after every iteration. This data is just a test data, the original data has more than 2.6 million rows.
You need a simple connect by clause -
CREATE TABLE NEW_TABLE AS
SELECT C.LOCATION, C.PARENT, LEVEL
FROM CTE C
START WITH PARENT = 'EEP'
CONNECT BY PRIOR LOCATION = PARENT;
Here is the fiddle

How to update a value in 1 table with the newly created PK of another table

I'm currently developing a project where I need to create a record in one table and leave the last column as NULL and update this later with the PK of another table (to establish a link).
Table 1 is a table for courses and table 2 is a table for the feedback form for each course.
The user first makes the course which is inserted in to table 1, THEN they make the feedback form which is inserted in to table 2.
Now I wanted to use a PK+FK relation here but, I can't set the FK of table 1 as the record hasn't yet been created in table 2.
For example, table 1 has the columns:
id(int)(PK), column1(int), column2(int), linkColumn(int)
Table 2 has the columns:
id(int)(PK), column1(int),...
I need to be able to make a record in table 1 and set linkColumn to NULL initially.
Then I need to create a record in table 2 and update linkColumn in table 1 with the Primary key of the newly created record in table 2.
How would I go about this?
Thanks!
Edit: I'm using PHP as the SQL handler
Use Triggers on insert for each row on Table2.
What Database are you using?
EDIT:
CREATE TRIGGER T_TABLE2_AI
AFTER INSERT
ON TABLE2 FOR EACH ROW
BEGIN
update Table1 set linkColumn = :new.ID where column1 = :new.column1;
END;

SQL Command to copy data from 1 column in table and 1 column in another table into a new table?

I had to make a new table to get the Include statement working in Entity Framework since EF was looking for a table called be_PostTagbe_Posts. I was using EF Code First from DB. But now the question is about SQL. I added one row of data and now the include works. But what I am looking for is a SQL command that can copy data from 1 column in 1 table and 1 column in another into the new be_PostTagbe_Posts table. In the be_Posts table I need the data in PostRowID to go into be_Posts_PostRowID and PostTagId to go into be_PostTag_PostTagID. Both be_PostTag_PostTagID and be_Posts_PostRowID are in the new be_PostTagbe_Posts table. I am not very good with SQL so not sure how to do this.
Edit: Thanks for the answers. I tried 2 separate queries but only data was inserted into the the be_PostTag_PostTagID while be_PostTag_PostRowID remained null.
And I tried this query which returned The multi-part identifier "be_PostTag.PostID" could not be bound.
INSERT INTO be_PostTagbe_Posts(be_PostTag_PostTagID, be_Posts_PostRowID)
SELECT be_PostTag.PostTagID, be_Posts.PostRowID
WHERE be_PostTag.PostID = be_Posts.PostID
EDIT:
This only inserted half the data - even 2 inserts leave one column null
INSERT INTO be_PostTagbe_Posts (be_Posts_PostRowID)
SELECT PostRowID FROM be_Posts;
INSERT INTO be_PostTagbe_Posts (be_PostTag_PostTagID)
SELECT PostTagID FROM be_PostTag;
And yet management studio tells me the query executed successfully but one column is still null. Weird.
Here are screenshots of the tables:
SELECT PostTagID AS be_PostTag_PostTagID, PostRowID AS be_Posts_PostRowID
INTO be_PostTagbe_Posts
FROM be_PostTag
Inner JOIN be_Posts
ON be_PostTag.PostID=be_Posts.PostID
That command created the new table with the 2 columns populated.
If i understand you ,you want to Copy Table Z's Column A to Table X And Table Z's Column B to Table Y.
If it is so, According to your question it is not clear about Table Structure of TableX and TableY
Assuming TableX And TableY to single ColumnTable [Apart from IdentityColumn] our query will be
INSERT INTO TableX
SELECT ColumnA FROM TableZ
INSERT INTO TableY
SELECT ColumnB FROM TableZ
Rest put your Entire Structure of Table To Get More Help Because These query are on Assumptions
There's not enough information in your question to give you a working example, but this would be the general syntax for INSERTing into a different table using a query SELECTing from two other tables.
INSERT INTO destination_table(wanted_value_1, wanted_value_2)
SELECT table_1.source_field_1, table_2.source_field_1
WHERE table_1.matching_field = table_2.matching_field
There has to be some sort of relationship between the two tables for the WHERE clause to work in that statement. I'm guessing based the little information you provided that there is a PostRowID field somewhere in the table that contains the tags such that your data would look similar to this in the tag table:
PostRowID PostTagID
--------- ---------
1 1
1 2
1 3
1 4
2 1
2 2
3 3
4 4
It sounds like you should use two sql statements:
Insert into `be_PostTagbe_Posts` (`be_PostTag_PostTagID`)
select `PostTagID` from POSTTAGIDTABLE
and
Insert into `be_PostTagbe_Posts` (`be_Posts_PostRowID`)
select `PostRowID` from POSTTAGIDTABLE
unless the items have some sort of relationship, then if you have a select statement that will select the merged data in two columns you can just do
Insert into `be_PostTagbe_Posts` (`be_PostTag_PostTagID`,`be_Posts_PostRowID`)
(select statement that selects the two items)

When value of a column changes in one table , a record should be inserted in other table in oracle DB

I want a SQL procedure / function to solve the below mentioned problem:
I have 2 tables - Table A and table B.
Table A has 3 columns - name, number and flag.
Table B has 2 columns - name and number.
When a value of flag column changes in table A, a record should be inserted in table B with the same values of name and number from table A.
How can I achieve this?
You can achieve this by using triggers.
Triggers are procedures that are stored in the database and are implicitly run, or fired, when something happens.
You can write triggers that fire whenever and INSERT, UPDATE, or DELETE operation is performed on a particular table or view.
General Syntax:
CREATE TRIGGER WRITE_TRIGGER_NAME_HERE
BEFORE UPDATE ON TABLE_A
FOR EACH ROW
BEGIN
WRITE_INSERT_STATEMENT_HERE_FOR_TABLE_B
END;