Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Question of the Day for 25 Jun 2007 Expand / Collapse
Author
Message
Posted Wednesday, June 20, 2007 12:05 PM
SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, May 18, 2007 3:36 PM
Points: 10,039, Visits: 1
Comments posted to this topic are about the Question of the Day for 25 Jun 2007 posted at http://www.sqlservercentral.com/testcenter/qod.asp?QuestionID=1065.
Post #375386
Posted Monday, June 25, 2007 9:47 AM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Tuesday, November 4, 2014 5:34 AM
Points: 737, Visits: 112
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.

Post #376312
Posted Monday, June 25, 2007 6:42 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, January 3, 2008 3:10 PM
Points: 4, 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.

Post #376451
Posted Tuesday, June 26, 2007 7:56 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Sunday, October 19, 2014 4:48 AM
Points: 1,327, Visits: 156

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
Post #376667
Posted Tuesday, June 26, 2007 12:43 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Wednesday, November 19, 2014 3:56 PM
Points: 6,086, Visits: 8,354

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
Post #376748
Posted Wednesday, June 27, 2007 5:32 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, January 3, 2008 3:10 PM
Points: 4, 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.
Post #376949
Posted Monday, July 2, 2007 11:14 AM
Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Monday, September 29, 2014 2:44 PM
Points: 3,475, Visits: 583

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

Post #378323
Posted Monday, April 2, 2012 8:07 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Wednesday, November 19, 2014 12:24 PM
Points: 3,384, Visits: 2,017
Interesting behavior.
Post #1276642
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse