Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.databases.ms-sqlserver > #92
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Message-ID | <4D96DE7A.1A4F8BAC@xs4all.nl> (permalink) |
| Date | Sat, 02 Apr 2011 10:29:46 +0200 |
| From | Gert-Jan Strik <sorrytoomuchspamalready@xs4all.nl> |
| Reply-To | sorry@toomuchspamalready.nl |
| X-Mailer | Mozilla 4.79 [en] (WinNT; U) |
| X-Accept-Language | en |
| MIME-Version | 1.0 |
| Newsgroups | comp.databases.ms-sqlserver, microsoft.public.sqlserver.programming |
| Subject | Re: Index or ORDER BY Using Another Table's Column |
| References | <imhcp6hrpjo1vkodlt0p487ghqe33ki59e@4ax.com> |
| Content-Type | text/plain; charset=us-ascii |
| Content-Transfer-Encoding | 7bit |
| Lines | 136 |
| NNTP-Posting-Host | 82.95.107.10 |
| X-Trace | 1301732986 news.xs4all.nl 41113 [::ffff:82.95.107.10]:55357 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.databases.ms-sqlserver:92 |
Cross-posted to 2 groups.
Show key headers only | View raw
> Can I do this (get at Accounts.ACOrd) in SQL Server without a join?
No. Since you need columns from two tables, you need both tables in your
query (joined on the foreign key relationship)
> Did I define my indexes properly?
When you declare a Primay Key constraint or Unique constraint, SQL
Server will automatically create unique indexes to enforce these
constraints. Because of that, you should not declare an identical index
yourself such as index ACUK or index SAUK.
Other than that, your indexes look fine.
If you are not already familiar with it, and want to learn more about
it, then it might be useful for you to read about the differences
between clustered and nonclustered indexes.
Good luck,
Gert-Jan
Gene Wirchenko wrote:
>
> Dear SQLers:
>
> For a start, I decided to model how I do banking. I have several
> bank accounts, and I split these up for setasides. When I list the
> subaccounts, the order I want is first by the Accounts order, then the
> Subaccounts order, and then the Subaccounts name.
>
> The SQL below sets up my database and does what I want. (Go to
> the very end for the SELECT statement in question.) Can I do this
> (get at Accounts.ACOrd) in SQL Server without a join? Did I define my
> indexes properly?
>
> ***** Start of Included Code *****
> /* Banking Learning Database */
> /* Version of 2011-04-01 14:42 */
>
> /* Database Setup */
>
> use master
> go
>
> drop database Banking
> go
>
> create database Banking
> go
>
> use Banking
> go
>
> /* Bank Accounts Table */
>
> create table Accounts
> (
> ACUK nvarchar(4) primary key, /* arbitrary length */
> ACOrd int,
> ACName nvarchar(40), /* arbitrary length */
> ACNr nvarchar(20) /* arbitrary length */
> )
>
> create unique index ACUK on Accounts(ACUK)
> create index ACOrd on Accounts(ACOrd,ACName)
>
> /* Subaccounts Table */
> /* Some are set-asides (S/A). */
>
> create table Subaccounts
> (
> SAUK nvarchar(4) unique, /* arbitrary length */
> SAOrd int,
> SAName nvarchar(40), /* arbitrary length */
> SAInACUK nvarchar(4), /* arbitrary length */
> foreign key (SAInACUK) references Accounts(ACUK)
> )
>
> create unique index SAUK on Subaccounts(SAUK)
> create index SAOrd on Subaccounts(SAOrd,SAInACUK,SAName)
>
> go
>
> /* Inserts */
>
> insert into Accounts
> (ACUK,ACOrd,ACName,ACNr)
> values
> ('INC',10,'Income','12345-6 01'),
> ('MAIN',20,'Main','12345-6 02'),
> ('SAVE',30,'Savings','12345-6 11')
>
> insert into Subaccounts
> (SAUK,SAOrd,SAName,SAInACUK)
> values
> ('INC',10,'Income','INC'),
> ('IBE',90,'Income Account Banking Expenses S/A','INC'),
> ('IINT',95,'Income Account Interest','INC'),
> ('PKT',10,'Pocket S/A','MAIN'),
> ('RENT',20,'Rent S/A','MAIN'),
> ('PH',30,'Phone S/A','MAIN'),
> ('MBE',90,'Main Account Banking Expenses S/A','MAIN'),
> ('MINT',95,'Main Account Interest','MAIN'),
> ('RES',10,'Reserves','SAVE'),
> ('ITSA',20,'Income Tax S/A','SAVE'),
> ('SINT',95,'Savings Account Interest','SAVE')
>
> /* Further Testing Inserts */
>
> insert into Accounts
> (ACUK,ACOrd,ACName,ACNr)
> values
> ('BOG',40,'Bogus','00000-0')
>
> insert into Subaccounts
> (SAUK,SAOrd,SAName,SAInACUK)
> values
> ('BOG2',20,'Bogus 2 S/A','BOG'),
> ('BOG1',10,'Bogus 1 S/A','BOG')
>
> go
>
> /* Get results. */
>
> select * from Accounts order by ACOrd,ACName
>
> select Subaccounts.*,ACOrd
> from Subaccounts
> join Accounts on SAInACUK=ACUK
> order by ACOrd,SAOrd,SAName
> ***** End of Included Code *****
>
> Sincerely,
>
> Gene Wirchenko
Back to comp.databases.ms-sqlserver | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Index or ORDER BY Using Another Table's Column Gene Wirchenko <genew@ocis.net> - 2011-04-01 14:47 -0700
Re: Index or ORDER BY Using Another Table's Column Gert-Jan Strik <sorrytoomuchspamalready@xs4all.nl> - 2011-04-02 10:29 +0200
Re: Index or ORDER BY Using Another Table's Column Gene Wirchenko <genew@ocis.net> - 2011-04-02 21:01 -0700
csiph-web