SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Question of the Day for 25 Jun 2007


Question of the Day for 25 Jun 2007

Author
Message
Aruna Gubba
Aruna Gubba
Right there with Babe
Right there with Babe (746 reputation)Right there with Babe (746 reputation)Right there with Babe (746 reputation)Right there with Babe (746 reputation)Right there with Babe (746 reputation)Right there with Babe (746 reputation)Right there with Babe (746 reputation)Right there with Babe (746 reputation)

Group: General Forum Members
Points: 746 Visits: 115
The script is not entirely correct. If the table creation doesn't explicitly use dbo., it creates the table under user credentials, NOT as dbo owned object. If you try to delete/truncate the table's data with dbo prefix, you will get an error

Cannot find the object "emp_mgr" because it does not exist or you do not have permissions.


al-435566
al-435566
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 3

CREATE TABLE emp_mgr ( emp char(30) PRIMARY KEY,

mgr char(30) NULL FOREIGN KEY REFERENCES emp_mgr(emp),

NoOfReports int DEFAULT 0 )

GO

INSERT emp_mgr(emp, mgr)

select 'Paul', 'Alice'

union all

select 'Joe', 'Alice'

union all

select 'Alice', 'Harry'

union all select 'Harry', NULL

go

In theory the above INSERT statement should throw a foreign constraint error because when it inserts the very first record (Paul, Alice) there is no record with 'Alice' primary key.


Mark Harr
Mark Harr
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1598 Visits: 191

In theory the above INSERT statement should throw a foreign constraint error...

I thought so, too. I guess this is an interesting application of the UNION ALL statement. It seems that all the statements are handled in a transaction, then the foreign key constraint is checked. If you run separate INSERT statements, then the anticipated foreign constraint error does occur. This will have to be filed away in the grey matter as an interesting trick or gadget when needed for a future application.





Mark
Hugo Kornelis
Hugo Kornelis
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11100 Visits: 12001

Hi Al,

>>In theory the above INSERT statement should throw a foreign constraint error because when it inserts the very first record (Paul, Alice) there is no record with 'Alice' primary key.<<

This is incorrect. There is only one INSERT statement, not a series of INSERT statements. It inserts the result of a query, that just happpens to be a bunch of UNION'ed together SELECT's for constants. However, there is no theoretic difference between this INSERT and something as

INSERT INTO Tab1 (col1, col2)
SELECT col3, col4
FROM Tab2
WHERE something = something_else;

In other words - the whole set of rows returned by the SELECT statement is inserted at once, not row by row.




Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
al-435566
al-435566
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 3
>In theory the above INSERT statement should throw a foreign constraint error because when it inserts the very first record (Paul, Alice) there is no record with 'Alice' primary key.

>>This is incorrect.

I've tried to run this INSERT statement in Sybase and it throws a foreign key constraint. Obviously SQL Server handles it differently and is intelligent enough to realise that given result set contains a record with 'Alice' primary key.

Would be interesting to know how Oracle treats this INSERT statement.
Yelena Varshal
Yelena Varshal
SSCarpal Tunnel
SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)SSCarpal Tunnel (4.6K reputation)

Group: General Forum Members
Points: 4632 Visits: 596

Hi,

I added ## to the table names to make tables temporary and it returned the following:

1. The message that it skips Foreign Key constraint that is not allowed on temp tables.

2. 4 rows affected. Which is expected message for the Insert statement

3. Msg 4701, Level 16, State 1, Line 1

Cannot find the object "emp_mgr" because it does not exist or you do not have permissions.

This is expected too because of the dbo prefix, discussed by other posters here.




Regards,
Yelena Varshal

Ken Wymore
Ken Wymore
SSCertifiable
SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)

Group: General Forum Members
Points: 5080 Visits: 2376
Interesting behavior.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search