Re: [sqlite] Proper SQL Syntax for a SELECT with LEFT JOIN Syntax for a 1 Primary Table, N Secondary Tables

2008-01-31 Thread Dennis Cote

Scott McDonald wrote:


Questions:
- How would you do what I am trying to do?
- Are these SELECT queries equivalent? Probably not as version 1 has a
sub-select query so this is a "2 pass" query, but is version 1 done in "1
pass" or "2 passes"?
- Is version 2 more efficient?
- For some reason, in version 2 I could not use the "USING" shortcut when
the joined fields had the same name so had to explicitly use the "ON" as
shown above, anybody know why?



Scott,

I prefer the second version myself. I think the intent is more obvious.

The following sqlite trace shows that the same query plan is used for 
queries using either view. I believe they are equivalent.


I believe the error produced by the third view below is a bug in sqlite.

SQLite should still have access to the UserStatusKey field from both the 
result of the first join and the USerStatusTable. The using clause in 
the first join causes standard SQL engines to generate a single new 
column that UserTypeKey, that is not considered to be a member of either 
of the tables, and that replaces the UserTypeKey from both of the joined 
tables.


SQLite version 3.5.4
Enter ".help" for instructions
sqlite> create table UserTable (
   ...> UserNumber  integer primary key,
   ...> UserTypeKey integer referneces UserTypeTable,
   ...> UserNametext,
   ...> UserStatusKey integer references UserStatusTable,
   ...> Passwordtext,
   ...> PasswordHint text
   ...> );
sqlite>
sqlite> create table UserTypeTable (
   ...> UserTypeKey integer primary key,
   ...> UserTypetext
   ...> );
sqlite>
sqlite> create table UserStatusTable (
   ...> UserStatusKey   integer primary key,
   ...> UserStatus  text
   ...> );
sqlite>
sqlite>
sqlite> CREATE VIEW UserView1 AS
   ...> SELECT UserNumber, UserType, UserName, UserStatus, Password, 
PasswordHint
   ...> FROM (SELECT UserNumber, UserType, UserName, UserStatusKey, 
Password,
   ...> PasswordHint FROM UserTable LEFT JOIN UserTypeTable 
USING(UserTypeKey))

LEFT
   ...> JOIN UserStatusTable USING(UserStatusKey);
sqlite>
sqlite> CREATE VIEW UserView2 AS
   ...> SELECT UserNumber, UserType, UserName, UserStatus, Password, 
PasswordHint

   ...> FROM UserTable
   ...> LEFT JOIN UserTypeTable ON 
UserTable.UserTypeKey=UserTypeTable.UserTypeKey

   ...> LEFT JOIN UserStatusTable ON
   ...> UserTable.UserStatusKey=UserStatusTable.UserStatusKey;
sqlite>
sqlite> explain query plan select * from UserView1;
0|0|TABLE UserTable
1|1|TABLE UserTypeTable USING PRIMARY KEY
2|2|TABLE UserStatusTable USING PRIMARY KEY
sqlite>
sqlite> explain query plan select * from UserView2;
0|0|TABLE UserTable
1|1|TABLE UserTypeTable USING PRIMARY KEY
2|2|TABLE UserStatusTable USING PRIMARY KEY
sqlite>
sqlite> CREATE VIEW UserView3 AS
   ...> SELECT UserNumber, UserType, UserName, UserStatus, Password, 
PasswordHint

   ...> FROM UserTable
   ...> LEFT JOIN UserTypeTable using(UserTypeKey)
   ...> LEFT JOIN UserStatusTable using(UserStatusKey);
SQL error: cannot join using column UserStatusKey - column not present 
in both tables


HTH
Dennis Cote

-
To unsubscribe, send email to [EMAIL PROTECTED]
-



[sqlite] Proper SQL Syntax for a SELECT with LEFT JOIN Syntax for a 1 Primary Table, N Secondary Tables

2008-01-31 Thread Scott McDonald

This is mainly a SQL syntax question but I am using the SQL for creating
views in SQLite database files.

Basic question: What is the proper SQL syntax for "Left Outer Joins" for 1
primary with N secondary (support) tables; I have come up with 2
possibilities that work but not sure which one is better or more efficient.
It is best to explain with an actual example from the system under
development:

Primary Table:

UserTable
- UserNumber (Primary Key)
- UserTypeKey (Foreign Key)
- UserName
- UserStatusKey (Foreign Key)
- Password
- PasswordHint

Secondary Tables:

UserTypeTable
- UserTypeKey (Primary Key)
- UserType

UserStatusTable
- UserStatusKey (Primary Key)
- UserStatus

You can probably guess where I am going next, but I am creating a "UserView"
where the "UserType" and "UserStatus" text fields are "joined" from the
respective tables, the foreign keys in the UserTable could be NULL hence the
need for "Left Outer Joins".

Version #1:
CREATE VIEW UserView AS
SELECT UserNumber, UserType, UserName, UserStatus, Password, PasswordHint
FROM (SELECT UserNumber, UserType, UserName, UserStatusKey, Password,
PasswordHint FROM UserTable LEFT JOIN UserTypeTable USING(UserTypeKey)) LEFT
JOIN UserStatusTable USING(UserStatusKey)

Version #2:
CREATE VIEW UserView AS SELECT UserNumber, UserType, UserName, UserStatus,
Password, PasswordHint
FROM UserTable
LEFT JOIN UserTypeTable ON UserTable.UserTypeKey=UserTypeTable.UserTypeKey
LEFT JOIN UserStatusTable ON
UserTable.UserStatusKey=UserStatusTable.UserStatusKey

Questions:
- How would you do what I am trying to do?
- Are these SELECT queries equivalent? Probably not as version 1 has a
sub-select query so this is a "2 pass" query, but is version 1 done in "1
pass" or "2 passes"?
- Is version 2 more efficient?
- For some reason, in version 2 I could not use the "USING" shortcut when
the joined fields had the same name so had to explicitly use the "ON" as
shown above, anybody know why?

Any feedback/help is much appreciated - thanks,
Scott McDonald









-
To unsubscribe, send email to [EMAIL PROTECTED]
-