Enterprise Architect can also generate the DDL from the physical schema, as shown below.
USE SCMS
GO
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id('FK_Venue Week Day Availability_Venue') AND OBJECTPROPERTY(id, 'IsForeignKey') = 1)
ALTER TABLE Venue Week Day Availability DROP CONSTRAINT FK_Venue Week Day Availability_Venue
GO
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id('FK_VenueTimePeriod_TimePeriod') AND OBJECTPROPERTY(id, 'IsForeignKey') = 1)
ALTER TABLE VenueTimePeriod DROP CONSTRAINT FK_VenueTimePeriod_TimePeriod
GO
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id('FK_VenueTimePeriod_Venue') AND OBJECTPROPERTY(id, 'IsForeignKey') = 1)
ALTER TABLE VenueTimePeriod DROP CONSTRAINT FK_VenueTimePeriod_Venue
GO
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id('TimePeriod') AND OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE TimePeriod
GO
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id('Venue') AND OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE Venue
GO
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id('Venue Week Day Availability') AND OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE Venue Week Day Availability
GO
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id('VenueTimePeriod') AND OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE VenueTimePeriod
GO
CREATE TABLE TimePeriod (
PeriodName nvarchar(50),
FirstDate smalldatetime,
PeriodStatus nvarchar(50),
LastDate smalldatetime,
StartTime smalldatetime,
EndTime smalldatetime,
timePeriodID int NOT NULL
)
GO
CREATE TABLE Venue (
Name nvarchar(50),
VenueSize nvarchar(50),
venueID int NOT NULL
)
GO
CREATE TABLE Venue Week Day Availability (
DayOfWeek int,
OpeningTime smalldatetime,
ClosingTime smalldatetime,
venue Week Day AvailabilityID int NOT NULL,
venueID int NOT NULL
)
GO
CREATE TABLE VenueTimePeriod (
venueTimePeriodID int NOT NULL,
timePeriodID int NOT NULL,
venueID int NOT NULL
)
GO
ALTER TABLE TimePeriod ADD CONSTRAINT PK_TimePeriod
PRIMARY KEY CLUSTERED (timePeriodID)
GO
ALTER TABLE Venue ADD CONSTRAINT PK_Venue
PRIMARY KEY CLUSTERED (venueID)
GO
ALTER TABLE Venue Week Day Availability ADD CONSTRAINT PK_Venue Week Day Availability
PRIMARY KEY CLUSTERED (venue Week Day AvailabilityID)
GO
ALTER TABLE VenueTimePeriod ADD CONSTRAINT PK_VenueTimePeriod
PRIMARY KEY CLUSTERED (venueTimePeriodID)
GO
ALTER TABLE Venue Week Day Availability ADD CONSTRAINT FK_Venue Week Day Availability_Venue
FOREIGN KEY (venueID) REFERENCES Venue (venueID)
GO
ALTER TABLE VenueTimePeriod ADD CONSTRAINT FK_VenueTimePeriod_TimePeriod
FOREIGN KEY (timePeriodID) REFERENCES TimePeriod (timePeriodID)
GO
ALTER TABLE VenueTimePeriod ADD CONSTRAINT FK_VenueTimePeriod_Venue
FOREIGN KEY (venueID) REFERENCES Venue (venueID)
GO
EXEC sp_addextendedproperty 'MS_Description', 'This is a generic time period that can be used by numerous specific time periods, like the time period a venue is available.
', 'Schema', dbo, 'table', TimePeriod
GO
|