Tag: syndicated

  • Defining FKs in CREATE TABLE–#SQLNewBlogger

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

    One of the important things that a database developer can do is to define Foreign Keys (FK) at table creation. This is a good time to do this as the referential integrity gets setup before any data is added and this can prevent issues later.

    This post shows the syntax for defining the FKs and adding them to your tables immediately.

    Build the Reference

    The first step is to ensure you have a table with a Primary Key (PK) that will be referenced. Let’s do that first. I’ve been looking to provide a database of SQL Server Builds, so let’s start with a table of versions.

    CREATE TABLE SQLServerVersion
    ( SQLServerVersionKey INT IDENTITY(1,1)
    , VersionName VARCHAR(200)
    , CONSTRAINT SQLServerVersionPK PRIMARY KEY (SQLServerVersionKey)
    )
    GO

    The PK in this table is what is referenced in the next table. This is required as a FK must refer to a PK in another table.

    Add the Reference

    When you build a child table, you may write the code like this:

    CREATE TABLE [dbo].[SQLServerBuilds]
    (
    [BuildKey] [int] NOT NULL IDENTITY(1, 1),
    [BuildNumber] [varchar] (30)  NULL,
    [BuildDescription] [varchar] (100)  NULL,
    [BuildKBArticleNumber] [varchar] (50) NULL,
    [BuildKBArticleURL] [varchar] (1000)  NULL,
    [SQLServerVersionKey] [int]  NULL
    ) ON [PRIMARY]
    GO

    However, in this case, you’ve ignored the FK that might link this table to the versions table. This means that a value could be entered in this table that doesn’t exist in the SQLServerVersion table.

    You might think this won’t happen with your application, but thousands, maybe millions, of developers have felt the same way. And they have junk data in their databases because of this.

    If there is a strong relationship, add the FK.

    Here’s how we do that in the CREATE TABLE statement. I’ll add a comma at the end and include a CONSTRAINT clause. I add the name and then the FOREIGN KEY keywords. Next I include the column from this table that is the FK with the References and the other table and column.

    CREATE TABLE dbo.SQLServerBuild
    (
         BuildKey INT NOT NULL IDENTITY(1, 1) ,
         BuildNumber VARCHAR(30) NULL ,
         BuildDescription VARCHAR(100) NULL ,
         BuildKBArticleNumber VARCHAR(50) NULL ,
         BuildKBArticleURL VARCHAR(1000) NULL ,
         SQLServerVersionKey INT NULL ,
         CONSTRAINT SQLServerBuild_Version_FK
             FOREIGN KEY (SQLServerVersionKey)
             REFERENCES dbo.SQLServerVersion (SQLServerVersionKey)
    ) ON [PRIMARY];
    GO

    This is the structure I tend to use, though sometimes I’ll move the CONSTRAINT clause directly below the actual column. This lets me see right away this is related to that column.

    I also avoid using this inline in the column as I can’t specify the constraint name, which I always want to do.

    SQLNewBlogger

    This is a core skill that database developers needed. If you know the syntax, this post would take about 10  minutes to structure and write. If not, maybe it’s 10 more to learn a bit.. Write your own and show you understand the design concepts.

    Reference

    Creating Foreign Key Relationships – https://docs.microsoft.com/en-us/sql/relational-databases/tables/create-foreign-key-relationships?view=sql-server-2017

  • Securing Test and Dev Environments at the SQL Privacy Summit

    Redgate Software is putting on a one day SQL Privacy  Summit on May 18 in London at the Grange Tower Bridge. This is a chance to see how you can ensure compliance with the GDPR while keeping up the modern pace of DevOps software development. Our goal is to educate you one the ways of protecting data and minimizing impact to business processes.

    Use the code “Steve” when you register to save 25% off the standard rate tickets. I’ll be there, along with some of the sharp minds from Redgate and some local consultancies to talk about the topics of data privacy and protection.

    We’ve designed the Summit to be for both technical and managerial staff that are working to ensure compliance with the GDPR. Our schedule is designed to help ensure that you lean from our research and experience to implement three key principles:

    • Map and maintain a living data catalogue of your data estate
    • Reduce your surface attack area by protecting data in dev and test environments
    • Implement ongoing monitoring to ensure your data is protected.

    It’s a great schedule of sessions delivered by some experienced speakers and a panel that will cover these topics and more. We even have a couple workshops to help you discuss concepts and best practices with peers.

    Register today (use the code “Steve”) and I’ll see you in London next month.

  • Watching the Sands of Suggestion in #SQLPrompt

    I enjoy themes, and when I ran across the SQL Prompt Treasure Island, I had to take a few minutes and go through it. I wrote about Code Snippet Code recently, and this post continues to move across the map.

    Incredible Suggestions

    The first thing that most people notice after they install SQL Prompt is the suggestions that pop up as you type SQL code. This was one of the first things that captivated my interest after I started using the product. Seeing lists of tables, of columns, of valid syntax, pop up as I type is so useful that I struggle when I don’t get the suggestion box. By default, this appears quickly, and one of my customizations is that I have slowed it slightly so that quick typos don’t pop the box if I get rid of them immediately.

    However, as the Treasure Map shows, it’s not just suggestions, but also the fact that I can hover over a suggestion and get more data, like the code that defines a table, view, or procedure. This is especially handy when using views, as I’ll look to see if I’m starting to nest views in queries.

    I do find that CTRL+Shift+D is one of the shortcuts that I often need. I may create a table or run some code in another tab, and I don’t get refreshed suggestions automatically. There is an experimental feature to auto refresh suggestions, but this means more polling of the database, and I try to avoid adding more load to processes that are running. The shortcut works well for me.

    The Dependencies Tip

    SQL Prompt has lots of features, and plenty that I don’t use. A few I don’t use because I don’t know about them. The dependencies tip is one of these. I didn’t know about it until I read the Treasure Map post, but now I think that’s a really cool features. If I’m looking to alter my schema, one of the important things to know is what dependencies I have. Certainly I could use SQL Search, but being able to quickly decide what objects I need to consider, or where I need to make other changes, is great.

    The Treasure Map describes this, but I had to experiment a bit to understand how this works. I’ve added a short animation to show this.

    dependencies

    Now that I know this, I’ll get the list, copy it, and use it as a TODO list of things to alter in this same commit. I’ll also know where to test changes before I actually commit this code to a shared repo where others will see it.

    There are plenty of other small features in this list, and you ought to experiment with these if you write a lot of T-SQL. You’ll find them to be helpful and handy.

  • Checking Tempdb with dbatools

    I really like the dbatools project. This is a series of PowerShell cmdlets that are built by the community and incredibly useful for migrations between SQL Servers, but also for various administrative actions. I have a short series on these items.

    In SQL Server 2016, the setup program was altered to better configure tempdb at installation time. This was in response to the observation that few people actually make any changes to the default configuration, which was suboptimal in SQL Server 2014-.

    Going through and checking all of the configurations you have isn’t easy, and isn’t necessarily the type of work that anyone wants to do. dbatools makes this really easy and quick with Test-DbaTempDbConfiguration.

    Using this cmdlet is easy. I’ll call this with an instance and get results of a number of checks that are useful for your tempdb configuration:

    2018-04-20 09_25_07-cmd - powershell

    This isn’t necessarily easy to read, so let’s add a Format-Table.

    2018-04-20 09_24_57-cmd - powershell

    That’s not great, as I’m missing the CurrentSetting field. I’ll add a SELECT and include the fields I want. I can even add multiple instances in here:

    2018-04-20 09_30_56-cmd - powershell

    Now I can scan through here, looking to see if my settings have deviated from the recommendations and best practices. This could easily be used to filter the results for items that don’t match, save the results as a CSV, and you now have a picklist of items to work on as you find time.

    dbatools is an essential tool for me. I’d urge you to download the module and experiment with the cmdlets.