SQL Server - geschachtelte abfrage oder join?

Asked By Martin
10-Sep-09 11:59 AM
hallo allerseits!
wie erstelle ich eine abfrage aus einer tabelle 'games' einer datenbank
'football' mit drei tabellen wie unten, die folgendes anzeigt:

Bremen Hamburg Weserstadium 01.07.2008 15:30 0 0

?
sollte ich die tabellen anders gestalten? oder l????t sich das machen?
danke f??r antwort!
Martin


CREATE TABLE [dbo].[Teams](
[ID] [int] IDENTITY(1,1) NOT NULL,
[NameFull] [nvarchar](50) COLLATE Latin1_General_CI_AS NOT NULL,
[NameAbbr] [nvarchar](50) COLLATE Latin1_General_CI_AS NOT NULL,
CONSTRAINT [PK_Teams] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

INSERT INTO [FootballTests].[dbo].[Teams]
([NameFull]
,[NameAbbr])
VALUES	(N'Werder Bremen',N'Bremen')

INSERT INTO [FootballTests].[dbo].[Teams]
([NameFull]
,[NameAbbr])
VALUES (N'Hamburger SV', N'Hamburg')


CREATE TABLE [dbo].[Stadiums](
[ID] [int] IDENTITY(1,1) NOT NULL,
[CityAndName] [nvarchar](50) COLLATE Latin1_General_CI_AS NOT NULL,
CONSTRAINT [PK_Stadiums] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

INSERT INTO [FootballTests].[dbo].[Stadiums]
([CityAndName])
VALUES
(N'Weserstadion')


CREATE TABLE [dbo].[Games](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Team1] [int] NOT NULL,
[Team2] [int] NOT NULL,
[Stadium] [int] NOT NULL,
[TimeRegular] [datetime] NOT NULL,
[Result1] [int] NOT NULL,
[Result2] [int] NOT NULL,
CONSTRAINT [PK_Games] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[Games]  WITH CHECK ADD  CONSTRAINT [FK_Games_Stadium]
FOREIGN KEY([Stadium])
REFERENCES [dbo].[Stadiums] ([ID])
GO
ALTER TABLE [dbo].[Games]  WITH CHECK ADD  CONSTRAINT [FK_Games_Team1]
FOREIGN KEY([Team1])
REFERENCES [dbo].[Teams] ([ID])
GO
ALTER TABLE [dbo].[Games]  WITH CHECK ADD  CONSTRAINT [FK_Games_Team2]
FOREIGN KEY([Team2])
REFERENCES [dbo].[Teams] ([ID])

INSERT INTO [FootballTests].[dbo].[Games]
([Team1]
,[Team2]
,[Stadium]
,[TimeRegular]
,[Result1]
,[Result2])
VALUES
(1, 2, 1, '1.7.2008 15:30', 0, 0)
CREATE TABLE
(1)
PRIMARY KEY
(1)
ALTER TABLE
(1)
FOREIGN KEY
(1)
NOT NULL
(1)
Nvarchar
(1)
Inner
(1)
CityAndName
(1)
  Christoph Ingenhaag replied...
11-Sep-09 02:44 AM
Hallo Martin,

ich w??rde es so machen:

select
t1.NameAbbr
,t2.NameAbbr
,s.CityAndName
,g.TimeRegular
,g.Result1
,g.Result2
from dbo.Games g
inner join dbo.Teams t1
on
g.Team1 = t1.ID
inner join dbo.Teams t2
on
g.Team2 = t2.ID
inner join dbo.Stadiums s
on
s.ID = g.Stadium


Viele Gr????e
Christoph
  Martin replied...
11-Sep-09 03:11 AM
Hallo Christoph,
danke Dir vielmals!
ich wu??te u.a. nicht, da?? ich variablen f??r tabellen anf??hren kann, bevor
sie definiert wurden...
einen sch??nen tag!
Martin
Create New Account
help
Date: 12 / 31 / 2008 10:56:11 * ** ** * / SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO create procedure [dbo].[sp_DTA_start_xmlprefix] as begin declare @startTags nvarchar(128) set @startTags = N'<DTAXML> <DTAOutput> <AnalysisReport> ' select @startTags end GO / * ** ** * Object: Table [dbo].[DTA_input] Script Date: 12 / 31 / 2008 10:56:59 * ** ** * / SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[DTA_input]( [SessionName] [sysname] NOT NULL, [SessionID] [int] IDENTITY(1, 1) NOT NULL, [TuningOptions] [ntext] NOT NULL, [CreationTime] [datetime] NOT NULL DEFAULT (getdate()), [ScheduledStartTime] [datetime] NOT NULL DEFAULT (getdate()), [ScheduledJobName] [sysname] NOT NULL DEFAULT (''), [InteractiveStatus] [tinyint
Problem with DROP CONSTRAINT SQL Server When I issue the following T-SQL 2008: ALTER TABLE ALIS.mc.LOCApp DROP CONSTRAINT LOCApp_Created_df I get the following errors: Msg 1781, Level 16 a DEFAULT bound to it. Msg 1750, Level 16, State 0, Line 1 Could not create constraint. See previous errors. I can see the constraint exists in Object Explorer. If I LOCTypeID (1) LOCAppID (1) Stored procedure (1) e r can you post DDL for the table? USE [ALIS] GO / * ** ** * Object: Table [mc].[LOCApp] Script Date: 12 / 19 / 2008 16:16:03 * ** ** * / SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [mc].[LOCApp]( [LOCAppID] [int] NOT NULL, [LOCAppVersion] [smallint] NOT NULL, [ApplyDate] [date] NOT NULL, [StatusCode] [int] NOT NULL, [StatusDate] [date] NOT
join tables, having, not exists (case end) . . .? SQL Server Hy, The first table is for coding cloth(name, etc.), the second table contain what is neded to create that cloth. Since I know that some things must be present to create a piece of cloth, I want a list of cloths cods that can't be Cab.DescricaoArtigo; The problem is that I need to verify each row in the detail table. but with "exists" and "case" after a true value the rest of the rows aren FichasTecnicasCabecalhoFichasTecnicasLinhaMP (1) LigacoesArtigosFichasTecnicasLinhaTecMP (1) SQL Server (1) EncomendasCabecalhoFichasTecnicasCabecalho (1) IDFichaTecnicaCabecalho (1) AaaaaFichasTecLinhaMP (1) FichasTecLinhaMP (1) CREATE INDEX (1) Hi, FULL OUTER JOIN will help you if I understand you correctly. On example. This query only return the articles that have in the first row of the table FichasTecLinhaMP one condition of HAVING clause! But what I want is all the articles that don't have this cods in the table FichasTecLinhaMP! And change not exist to exists or not like to like don't work
Foreign key constraints with null SQL Server In Sql Server 2008, I am using the Diagrammer to set my relationships and need to set up foreign key relationships but also allow null in the field. How do I do that? The code is coming out: ALTER TABLE [dbo].[PAYMENT] WITH CHECK ADD CONSTRAINT [FK_ _Payment_AccountType] FOREIGN KEY([AccountTypeID]) REFERENCES [dbo].[ACCOUNT_TYPE] ([AccountTypeId]) GO
Management Studio Express multiple cascade paths with cascade updates and deletes relationship successfully setup from Table A col1 -> TableB col1 and Table A col2 -> TableB col2. Am I dreaming ? Has someone tried this ? The relationship setup successfully 1) SQL Server 2005 (1) SQL Server (1) MySQL (1) Oracle (1) Stored procedure (1) CREATE TABLE (1) PRIMARY KEY (1) You should not have them if your schema is designed properly. I tell people to take an ER diagram and reduce it to table names and PK-FK paths to make a flow diagram. This lets you see directed 2005 it does not work. Some code for testing: - - 1) self-reference within the same table - - works in IBM DB2 LUW 9.5 - no warning is displayed - - does not work in