Category: Blog

  • Adding a Foreign Key in the CREATE TABLE statement–#SQLNewBlogger

    I had someone ask this question recently and had to double check the syntax myself, so I thought this would make a nice SQL New Blogger post.

    Another post for me that is simple and hopefully serves as an example for people trying to get blogging as #SQLNewBloggers.

    Defining a Foreign Key

    Most people define a foreign key like this:

    ALTER TABLE [dbo].[OrderLine]  WITH CHECK ADD  CONSTRAINT [FK_OrderLine_Order] FOREIGN KEY([OrderID])
    REFERENCES [dbo].[Order] ([OrderID])
    GO

    This assumes I’ve added a table called dbo.Order with a PK of OrderID.

    However, I can do this in the CREATE TABLE statement, like shown below. I add a new section after a column with the CONSTRAINT keyword. Then I name the constraint, which is always a good practice. I can then add the FK keyword, the column and the references that connects this child column to the parent column.

    CREATE TABLE dbo.OrderLine
    ( OrderLineID INT NOT NULL CONSTRAINT OrderLinePK PRIMARY KEY
    , OrderID INT
    , Qty INT
    , Price NUMERIC(10,2)
    , CONSTRAINT FK_OrderLine_Order FOREIGN KEY (OrderID) REFERENCES dbo.[Order](OrderID)
    )
    GO

    Easy to do and this keeps my code clean.

    Note that if I script this out in SSMS, I’ll get this:

    CREATE TABLE [dbo].[OrderLine](
    [OrderLineID] [int] NOT NULL,
    [OrderID] [int] NULL,
    [Qty] [int] NULL,
    [Price] [numeric](10, 2) NULL,
    CONSTRAINT [OrderLinePK] PRIMARY KEY CLUSTERED
    (
    [OrderLineID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    
    ALTER TABLE [dbo].[OrderLine]  WITH CHECK ADD  CONSTRAINT [FK_OrderLine_Order] FOREIGN KEY([OrderID])
    REFERENCES [dbo].[Order] ([OrderID])
    GO

    Nothing wrong with that, but knowing both syntaxes is a good idea. Plus, if you know this is a child column, define it right away.

    SQL New Blogger

    This is a post that took me about 15 minutes to write. I had to create and drop the tables a few times and verify I had the syntax correct, and then explain and format things.

    This is a core skill for a DBA or developer. You ought to know how to define a FK and use them where appropriate. Write your own post to show how to build a FK for some scenario that you work with in your job, or in a project.

  • Suppressing the FOMO

    I saw a post recently on Twitter/X/whatever we call it. It was for a DevOps in a Day from Redgate taking place in Jacksonville.

    2023-10-19 16_17_28-way0utwest ️ (He_Him_His) on X_ _@ReviewMyDB @redgate @GFritchey Enjoy. Sorry

    I’ve been a part of the tour this fall, and when we announced the Jacksonville date, Jeff reached out and asked if we could get together. I told him I wasn’t coming, and he asked why not. At the time I said I was busy and wanted the week off, which was true. I’m not sure Jeff felt that made sense, but Grant was going, so everything was fine.

    However

    When I saw the post, I felt like I should be there and I was missing out. I slightly regretted not going. After all, the week this event took place, I was at home, with no travel.

    That was my first M-F week at home since July 10-14. I was trying to protect it, and I think I did the right thing by not going and letting Grant handle things, but I still felt bad. I had some FOMO (Fear of missing out).

    FOMO

    I find FOMO drives a lot of people to do extra things. They do more work, they go places, they get caught up with friends/coworkers/family/etc. because they want to participate in something that’s fun or valuable or helpful to their career.

    I get it. I feel the same way, but I have learned often to step back from situations. This year, I knew the fall would be tough with travel, but I was also excited and looking forward to the tour. I wanted it to go well and for Redgate to do more in 2024.

    However.

    I don’t need to do it all. Grant can handle the day, and if not, Ryan can help. We’ve already talked about the need to plan further in advance and split the load. There is plenty of work.

    We’re Not Saving Babies

    My wife has used this phrase a lot. Most of us aren’t saving babies. If you are, then work hard, heck work 100 hours a week and then take time off, but save babies.

    Most of us do work that could wait until tomorrow. We can let someone else do some of the work. Even if someone does a better job, often there is so much work that they company will still need you to do something.

    Certainly Redgate will still use me at some events, and I’ll still travel.

    I just don’t need to do all the events.

    I keep telling myself that I’m certainly not saving babies, and there is plenty of other work. And, I’ve been to Jacksonville (and many other places) this year. I can skip a few trips.

    Side Note

    Even as I had FOMO, I had just sent an email to the US offices thanking them for an invitation to the Christmas parties, but declining to go since I didn’t want to get on another airplane this year. I still have two trips after this week, and I’m limiting my life to those.

  • A New Word: Funkenzwangsborstellung

    funkenzwangsborstellung – n.  the primal trance of watching a campfire in the dark.

    I am not a big camper, but 3-4 times a year we usually go out with horses. Usually my wife rides and I relax, play guitar, and cook. Often we do build a campfire if the area isn’t under restrictions, and it’s nice to sit and stare into the fire.

    I don’t know what it’s relaxing, but I do enjoy the funkenzwangborstellung.

    From the Dictionary of Obscure Sorrows

  • More Vacation

    It’s getting close to the end of the year, and I’m trying to take off a few days here and there to close out the year. It’s been a long year, and I don’t want to leave any behind if I can avoid it. It’s always good to get away from work, and I have worked on accepting that view.

    This week I’m back in New York to visit my daughter again and see some of the last few volleyball matches I’ll see her play.

    See you next week.