Oracle INSERT multiple rows - sql

My statement will insert two out of the four student IDs. The ones that are inserted are Id's that already exist with another section number. I cant figure out why the other two are not inserted.
INSERT INTO enrollment
(student_id,section_id,enroll_date,created_by,created_date,modified_by,modified_date)
SELECT
student_id,'48',SYSDATE,'KABEL',SYSDATE,'KABEL',SYSDATE
FROM enrollment
WHERE student_id IN ('375','137','266','382');

There is nothing much wrong in your query. You are typo or by mistake choose INSERT table and SELECT query from one table as this does not make any sense, to select from one table and insert into same table.
Your select and Insert statement using the same table enrollment.
These should be two different table.
INSERT INTO enrollment <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<-------Here Problem
(student_id,section_id,enroll_date,created_by,created_date,
modified_by,modified_date)
SELECT
student_id,'48',SYSDATE,'KABEL',SYSDATE,'KABEL',SYSDATE
FROM enrollment <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<-------Here Problem
WHERE student_id IN ('375','137','266','382');

Related

Insert into multiple rows on Multiple tables

Inserting rows into table one by one is more hardy and tedious instead of inserting rows into more than one tables at one time with just in single SQL query
Because I have 10 tables in which I need to insert rows so it would be more boring to put rows only in one table at a time rather than all rows inserts at all tables
So please suggest me a better query to insert all rows at all tables in one time
For multitable insert you can use the below syntax if you have to insert a limited set of records at 1 go:
INSERT ALL
INTO AA(A,B,C,D,E,F) VALUES (1,2,3,4,5,6)
INTO AB(A,B,C,D,E) VALUES (5,4,3,4,2)
SELECT * FROM DUAL;

SQL inserting rows from multiple tables

I have got an assignment. We have been given a table, MAIN_TABLE, which has a column patient_id as foreign key.
I need to make a separate table named patient which has patient_id as a primary key along with some other attributes such as name and address.
I did successfully create schema of this table. Now there is a serious problem I am facing. After creating this table I used insert statement to insert values for name and address from a dummy table.
Till this point everything works fine. However, the column patient_id is still empty rather I have set it to 0 by default.
Now the problem is that I need to get values into this column, patient_id, from the patient_id column of MAIN TABLE.
I can't figure out how do I do this? I did try to use:
UPDATE patient
SET patient_id=(select id from MAIN_TABLE)
BUT this gives me error that multiple rows returned which does make sense but what condition do I put in where clause then?
That sounds strange. How can there be a table MAIN_TABLE with a foreign key patient_id but the master table patient does not exist. Where do that patient_ids in MAIN_TABLE come from?
I suggest not to insert your data from a dummy table alone and then try to update it. But insert it with both - the MAIN_TABLE and the dummy table joined. If you can not join them. You would also not be able during the update.
So since i think they have no connected primary/foreign keys the only way to join them is using a good business key. Do you have a good business key?
You are talking about persons. So First Name, Last Name, Birth Day, Address often is good enough. But you have to think about it.
With your given data I can only give you some kind of meta insert statement. But you will get the point.
Example:
insert into patient (col1, col2, col3)
select
a.colA,
a.colF,
b.colX
from
dummy_table a
inner join MAIN_TABLE b on a.colN=b.colA and a.colM=b.colB
And: If patient_id is your primary key in patient you should ensure that it is even not possible to have duplicate values or null in this column. And you should use constraints to ensure your data integrity.
http://docs.oracle.com/cd/B19306_01/server.102/b14200/clauses002.htm

Is it possible to insert data into table using two select statements in one query?

I am working with databases in which I have 3 tables one is skill table the other one is experience and the third one is Experience_skill table in this table I have foreign keys now the question is both foreign keys are primary keys as well. Let say I am storing data into skill table as well as in experience table how can I insert the the both keys data there in Experience_skill table. I have tried following queries.
insert into Experience_skill(eid, Skill_Id)
select eid
from Experience
where eid=2
union
select Skill_Id
from Skills
where Skill_Id=2
error I get:
The select list for the INSERT statement contains fewer items than the insert list. The number of SELECT values must match the number of INSERT columns.
Than i tried this one.
insert into Experience_skill(eid)select eid from Experience where eid=2
it gives me this error:
Cannot insert the value NULL into column 'Skill_Id', table 'resume.dbo.Experience_skill'; column does not allow nulls. INSERT fails.
Please help me out
here are the snapshots of the table first snap shot is of skill table
the second one is experience table
And this one is EXperience_skill table where i have my foreign keys
When using INSERT INTO, you need to supply ALL of the columns in the destination table (that do not allow NULLs) in each ROW of the SELECT.

trigger writing for different column names in two different tables

I am creating a trigger and understand how to do this if the columns in two different tables have the same name. Ex, I write a trigger that if a new record is added in the salary column for table A to take the salary from table A and insert that as salary in table B.
What I don't understand how to do, is populate that data into table B if that table has a different column name. So I have table A that the column name is salary and table B where the column name is pastsalary.
How do I write the trigger to be able to do that?
I don't see the difference between the two.. Your insert statment would just reflect the column you want to insert into:
insert into tableB(pastsalary) values (:new.salary);

What SQL query do I need in order to add lots of empty rows to a table at once?

I understand that what I am asking for may not make a lot of sense, but I none the less have a particular need for it. I have a table that has 500 rows in it. I have another table that has 500 more rows, that I need to merge into the first table. The easiest way I know how to do that is to add 500 rows to the first table, and then use an update statement because then I have a primary key to use to pair the first and second tables.
So how can I add 500 blank rows to my first table? I've been trying to think of a query that would do that, but haven't been able to come up with anything...
You can insert to one table from another table:
INSERT INTO suppliers (supplier_id, supplier_name)
SELECT account_no, name
FROM customers
WHERE city = 'Newark';
You can use insert into statement:
SQlite: select into?
As long as the tables contain the same data structure, you can use a simple query to insert them into your table:
INSERT INTO tableOne SELECT * FROM tableTwo
If you have to manually map the fields, you'll have to change it to the field level insert, such as:
INSERT INTO tableOne(columnOne,columnTwo) SELECT column3, column4 FROM tableTwo
You can add the standard WHERE statements to these as well.
Hope that helps.