GithubHelp home page GithubHelp logo

apress / pro-sql-server-relational-database-design-and-implementation Goto Github PK

View Code? Open in Web Editor NEW
7.0 4.0 6.0 5.28 MB

Source code for 'Pro SQL Server Relational Database Design and Implementation' by Louis Davidson

Home Page: https://www.apress.com/9781484264966

License: Other

TSQL 100.00%

pro-sql-server-relational-database-design-and-implementation's Introduction

Apress Source Code

This repository accompanies Pro SQL Server Relational Database Design and Implementation by Louis Davidson (Apress, 2020).

Cover image

Download the files as a zip using the green button, or clone the repository to your machine using Git.

Releases

Release v1.0 corresponds to the code in the published book, without corrections or updates.

Contributions

See the file Contributing.md for more information on how you can contribute to this repository.

pro-sql-server-relational-database-design-and-implementation's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

pro-sql-server-relational-database-design-and-implementation's Issues

Ch7 Misleading FK Constraint name

In Chapter 7. Physical Model Implementation, in the text following after Figure 7-5 Messaging model for reference is ALTER TABLE statement to implement FK in the table MessagingUser; relationship between MessagingUser and AttendeeType.
ALTER TABLE Attendees.MessagingUser
ADD CONSTRAINT FKMessagingUser$IsSent$Messages_Message
FOREIGN KEY (AttendeeType)
REFERENCES Attendees.AttendeeType(AttendeeType)
ON UPDATE CASCADE
ON DELETE NO ACTION;
The name of the CONSTRAINT FKMessagingUser$IsSent$Messages_Message is however a bit misleading. It suggests rather (one of) the relationships between tables MessagingUser and Message (in the Figure „is sent“) but in reality the „categorizes“ relationship is being established.

Ch9 SET ANSI_WARNINGS OFF causes error

In the downloaded file Chapter 09.sql there is the following block of code starting with SET ANSI_WARNINGS OFF wich causes error, when I switch it to ON it works OK.
SET ANSI_WARNINGS OFF;
DECLARE @query varchar(8000);
SELECT
@query = 'SELECT Equipment.EquipmentTag,Equipment.EquipmentType ' + (
SELECT DISTINCT
',MAX(CASE WHEN EquipmentPropertyType.name = ''' +
EquipmentPropertyType.name + '''
THEN CAST(Value AS ' +
EquipmentPropertyType.TreatAsDatatype + ') END) AS
[' +
EquipmentPropertyType.name + ']' AS [text()]
FROM
Hardware.EquipmentPropertyType
FOR XML PATH('') , type ).value('.', 'NVARCHAR(MAX)') + '
FROM Hardware.EquipmentProperty
JOIN Hardware.Equipment
ON Equipment.EquipmentId =
EquipmentProperty.EquipmentId
JOIN Hardware.EquipmentPropertyType
ON EquipmentPropertyType.EquipmentPropertyTypeId
= EquipmentProperty.EquipmentPropertyTypeId
GROUP BY Equipment.EquipmentTag,Equipment.EquipmentType '
EXEC (@query);

Ch 8 Abundancy checks in Trigger? Range Checks over Multiple Rows

In the Range Checks over Multiple Rows section there is an example with 2 tables Accounting.Account and Accounting.AccountActivity using trigger to prevent activities that would cause negative balance of the individual accounts. It seems to mee (and not only in this case) that there is a double check if there exist inserted rows. The First is the statement “ IF @rowsAffected = 0 RETURN;” and the second in the validation section:
WHERE EXISTS (SELECT *
FROM inserted
WHERE inserted.AccountNumber =
AccountActivity.AccountNumber)
I have commented that EXISTS (see below) and it works OK. What is the reason of this second check. I cannot imagine situation where it would be necessary.
Follows the part of the Trigger ( with commented EXISTS under LEFT OUT AND WORKING ):

CREATE TRIGGER Accounting.AccountActivity$InsertTrigger
ON Accounting.AccountActivity
AFTER INSERT AS
BEGIN
SET NOCOUNT ON;
SET ROWCOUNT 0; --in case the client has modified the rowcount
--use inserted for insert or update trigger, deleted for update
--or delete trigger count instead of @rowcount due to merge behavior
-- that sets @rowcount to a number that is equal to number of merged
-- rows, not rows being checked in trigger
DECLARE @msg varchar(2000), --used to hold the error message
--use inserted for insert or update trigger, deleted for update
--or delete trigger count instead of @rowcount due to merge behavior
--that sets @rowcount to a number that is equal to number of merged
--rows, not rows being checked in trigger
@rowsAffected int = (SELECT COUNT(*) FROM inserted);

--no need to continue on if no rows affected
IF @rowsAffected = 0 RETURN;

BEGIN TRY

--[validation section]
--disallow Transactions that would put balance into negatives
IF EXISTS ( SELECT AccountNumber
FROM Accounting.AccountActivity AS AccountActivity
--LEFT OUT AND WORKING
--WHERE EXISTS (SELECT *
-- FROM inserted
-- WHERE inserted.AccountNumber =
-- AccountActivity.AccountNumber)
GROUP BY AccountNumber
HAVING SUM(Amount) < 0)

Chapter 7 – Database Create Objects.sql - missing FK constraints

In the downloaded file Chapter 7 – Database Create Objects.sql two following constraints of FK are missing:
ALTER TABLE Messages.Message
   ADD CONSTRAINT FKMessagingUser$Sends$Messages_Message FOREIGN KEY
     (MessagingUserId) REFERENCES Attendees.MessagingUser(MessagingUserId)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION;
ALTER TABLE Messages.Message
   ADD CONSTRAINT FKMessagingUser$IsSent$Messages FOREIGN KEY
     (SentToMessagingUserId) REFERENCES
                                   Attendees.MessagingUser(MessagingUserId)
        ON UPDATE NO ACTION
        ON DELETE NO ACTION;

FK missing in Messages.Message

I couldn't find transport of PKs from the table Attendees.MessagingUser to Messages.Message in the downloaded script for Chapter 8, Chapter 7.sql, Chapter 7 – Database Create Objects.sql. Further more, in the script for FK in Attendees.MessagingUser, ie relation between Attendees.AttendeeType and Attendees.MessagingUser, the name of the constraint does not seem in accordance of the philosophy of the book; AttendeeType should "categorize" MessagingUser, wouldn't it be so? Instead of FKMessagingUser$IsSent$Messages_Message sth like FKMessagingUser$Categorizes$Attendees_AttendeeType.

ALTER TABLE Attendees.MessagingUser
ADD CONSTRAINT FKMessagingUser$IsSent$Messages_Message
FOREIGN KEY (AttendeeType) REFERENCES Attendees.AttendeeType(AttendeeType)
ON UPDATE CASCADE
ON DELETE NO ACTION;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.