inserting data in two tables Simultaneously - sql

I have two table in my database , Position and Reservation that ID of Position is foreign key in Reservation. I want to insert data in reservation table but before this , data must be inserted to position table simultaneously . how can i do this??? can I use triggers or store procedure???

I assume your tables look like this:
position: int id
reservation: int id, int position_id
First you've got to have a look at your foreign key position_id. If it has a NOT NULL constraint, you won't be able to create a reservation entry without before creating a position entry.
Using "normal" SQL queries, it's not possible to insert data into two tables with a single query.
Why do you need it to happen within one query? Because of data integrity? Then use transactions!

Related

Combine results from multiple views and add identifier

I have a view that selects and joins data from several tables.
I have this same view in multiple databases on different servers. (it's part of the same application installed on various servers)
What I'm trying to do is use the 'Export Data' wizard to create an SSIS package that copies the data from these views to a single data warehouse database.
However, since I can't guarantee that there won't be identical rows in the the views, I want to add an ID column in the data warehouse db. But I can't seem to get it to work.
Usually when you want to add an autoincrement ID, one simply inserts 'NULL' into that column. So I've added a 'NULL' value to the select of the view. And I've added an ID column to the destination table, with Identity and auto-increment on.
However, when I run the Export Data wizard, it gives an error
'The value violated the integrity constraints for the column.'
Does anyone have an idea how to combine data from different views on different db servers and add a unique identifier in the destination table?
Cheers, CJ
You can't insert NULL into the Primary Key. Just don't insert anything into the ID column if you have it as auto incremented. Example:
CREATE TABLE test_table
(
ID int IDENTITY(1,1) PRIMARY KEY,
test varchar(255) NOT NULL
);
INSERT INTO test_table(test )
VALUES ('some value');
And ID will be set to 1 for this record.

Combine the Data of two SQL databases to one

I have two SQL databases with the same schema. Both have different Data but are using the same primary keys. I want to add the Data from one database to the second one, but all solution I found just update the rows with the same primary keys and dont attach them at the end.
Anyone has a solution?
If your primary key column is an ID field that uses an auto-incrementing sequence (if not, then how would you choose a different primary key for overlapping records anyway?), you should just be able to insert the records from the first schema, excluding the ID in your select query.
For example:
Table Schema:
ID INT(10)
Name VARCHAR(20)
Desc TEXT
SQL
INSERT INTO schema2.table (Name, Desc)
SELECT Name, Desc
FROM schema1.table

SQL self calculated field

I have two tables which are connected by m2m relationship.
CREATE TABLE words
(
id INT PRIMARY KEY,
word VARCHAR(100) UNIQUE,
counter INT
)
CREATE TABLE urls
(
id INT PRIMARY KEY,
url VARCHAR(100) UNIQUE
)
CREATE TABLE urls_words
(
url_id INT NOT NULL REFERENCES urls(id),
word_id INT NOT NULL REFERENCES words(id)
)
and i have counter field in words table. How can i automize proccess of updating counter field which is responsible for calculating how much rows stored in urls_words with particular word.
I would investigate why you want to store this value. There may be good reasons, but triggers complicate databases.
If this is a "load-then-query" database, then you can update the count when you load data -- presumably at some frequency such as once a day or once a week. You don't need to worry about triggers.
If this is a transactional database, then triggers would be needed and these add complexity to the processing. They also lock tables when you might not want them locked.
An alternative is to have an index on urls_words(word_id, url_id). This would greatly speed the calculation of the count when you need it. It also does not require triggers or locks on multiple table during an update.
Create a trigger on urls_words table which updates the counter column on words table every time a change is made (ie update, insert, delete).

When adding data to a table, it doesn't show up in the Foreign key of the other table

I have a table COURSE which has two attributes COURSECODE (PK) and COURSENAME
I also have another table COURSEUNIT, which has three attributes COURSECODE (PK) (FK) UNITCODE (PK) (FK) and CREDIT .
When I add data to the table COURSE it doesn't add the data to the COURSEUNIT table. What is the problem?
Foreign key does not mean that you get one row for each key in the referenced table. It only means that any row in the COURSEUNIT table must reference an existing row in COURSE.
Those are 2 different tables and you have to manage the relation, you have to insert the data first in the course table then take the id that will be generated from this insert and make another insert in the coursecode table, they will not be inserted automatically.
No problem there, that's how SQL works, you'll need a second INSERT statement to populate the COURSEUNIT table.
You need to write the query to populate the second table, how else will it know what values besides the FK to put in it? Child tables do not auto populate in any database.

How to move data between multiple database's table while maintaining foreign-key relationships/referential integrity?

I'm trying to figure out the best way to move/merge a couple tables of worth of data from multiple databases into one.
I have a schema similar to the following:
CREATE TABLE Products(
ProductID int IDENTITY(1,1) NOT NULL,
Name varchar(250) NOT NULL,
Description varchar(1000) NOT NULL,
ImageID int NULL
)
CREATE TABLE Images (
ImageID int IDENTITY(1,1) NOT NULL,
ImageData image NOT NULL
)
With a foreign-key of the Products' ImageID to the Images' ImageID.
So what's the best way to move the data contained within these table from multiple source databases into one destination database with the same schema. My primary issue is maintaining the links between the products and their respective images.
In SQL Server, you can enable identity inserts:
SET IDENTITY_INSERT NewTable ON
<insert queries here>
SET IDENTITY_INSERT NewTable OFF
While idenitity insert is enabled, you can insert a value in the identity column like any other column. This allows you to just copy the tables, for example from a linked server:
insert into newdb.dbo.NewTable
select *
from oldserver.olddb.dbo.OldTable
I preposition the data in staging tables (Adding a newid column to each). I add a column temporarily to the table I'm merging to that is Oldid. I insert the data to the parent table putting the currect oldid inthe oldid column. I use the oldid column to join to the staging table to populate the newid column in the staging table. Now I have the New FK ids for the child tables and ccan insert using them. If you have SQL server 2008, you can use the OUTPUT clause to return the old and newids to a temp table and then use from there rather than dding the column. I prefer, to have the change explicitly stored ina staging table though to troubleshoot issues in the conversion. At the end nullout the values inteh oldid column if you are then going to add records from a third database or drop it if you are done. Leave the staging tables in place for about a month, to make research into any questions easier.
In this case you could move the images and then move the products. This would ensure that any image a product has a reference to will already be in place.