Tag: SQLNewBlogger

  • Finding Inconsistent Key Values–#SQLNewBlogger

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

    I was reading Iris Classon’s blog recently and ran across a post on her day at work. I think it’s a fantastic post that does help younger people understand what a job is like. I’m looking forward to seeing more.

    One quick thing struck me in the post, which was that she has clients that are missing key value settings in a table. I’ve dealt with this, and want to write more, but a quick post on just finding out that there are missing settings.

    Finding the Data Inconsistencies

    Each client should have a set of key value pairs in a settings table. However, because of application problems, not every client does. In the sample data (below), there are 8 clients, each of which should have 5 values in the GlobalSettings table. However, there are only 20 rows in this table when there should be 40.

    This is the type of issue I’ve had occur in an application, especially one that evolves over time. We add a key-value item to the application, which new clients get, but older ones are never populated. This is the same type of issue I saw in a post by Iris Classon.

    How can I find the items that don’t match? One simple way is to count the values, but include a HAVING clause to limit the results. If I want to see who has all the values, I can do this:

    SELECT gs.ClientID, COUNT(*)
    FROM dbo.GlobalSetting AS gs
    GROUP BY gs.ClientID
      HAVING COUNT(*) = 5

    In my sample data, this returns one row, for Client 1. If I change the HAVING clause to < 5, I get the other seven rows.

    2018-06-29 14_46_00-SQLQuery1.sql - (local)_SQL2016.sandbox (vstsbuild (56))_ - Microsoft SQL Server

    There are other considerations here, and this isn’t the best way that you might ensure you have the values. I might have a table, or a derived table that ensures I’m checking the right 5 values.

    I’ve written more about this in an article at SQLServerCentral.

    The Setup

    I built a couple quick tables and added data with this script. Note that there are 8 clients, and that each has a series of settings in a table. There are 5 possible settings (Position, Height, Weight, Number, College)

    CREATE TABLE Client
    (ClientKey INT IDENTITY (1,1) NOT NULL CONSTRAINT ClientPK PRIMARY KEY 
    , ClientName VARCHAR(200)
    , ClientStatus TINYINT)
    go
    CREATE TABLE GlobalSetting
    ( GlobalSettingKey INT IDENTITY (1,1) NOT NULL CONSTRAINT GlobalSettingPK PRIMARY KEY 
    , ClientID INT NOT NULL CONSTRAINT GlobalSettingFK_Client_ClientID FOREIGN KEY REFERENCES Client
    , GlobalSettingName VARCHAR(100)
    , GlobalSettingValue VARCHAR(500)
    )
    GO
    INSERT client VALUES ('Shaquil', 1), ('Von', 1), ('Bradley', 1), ('Shane', 2), ('Todd', 2), ('Jerrol', 3), ('Jeff', 3), ('Josey', 3)
    
     Position, College, Height, weight, number
    INSERT dbo.GlobalSetting
    (
        ClientID ,
        GlobalSettingName ,
        GlobalSettingValue
    )
    VALUES
      (1, 'Position', 'OLB')
    , (1, 'Weight', '250'),
    (1, 'College', 'CSU') , (1, 'Height', '74') , (1, 'Number', '48') , (2, 'Weight', '250') , (2, 'Number', '58') , (2, 'College', 'Texas A&M') , (3, 'Height', '76') , (3, 'Weight', '269') , (4, 'Position', 'OLB') , (4, 'College', 'Missouri') , (4, 'Height', '75') , (5, 'Weight', '230') , (5, 'Number', '51') , (5, 'College', 'Sacramento St') , (6, 'Weight', '235') , (6, 'Position', 'LB') , (7, 'Weight', '249') , (8, 'Number', '47')

    SQLNewBlogger

    This only took about 10 minutes to write, though I had to build the tables and data. Of these, the data took the longest, because I had to look up the values Winking smile.

    This is a basic example of checking data in a business situation. I might write about this in my job, perhaps showing a daily integrity check or a custom metric that I use to ensure the system is working. You can do the same thing and ensure that your data is correct.

  • Getting SQL Agent going for SQL Server on Linux–#SQLNewBlogger

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

    It’s actually a simple procedure, but I thought I’d write a short note to help me remember. The procedure is documented in Books Online.

    When I first started playing with this version, I noticed that SQL Agent was disabled. That’s not great, since SQL Agent is a great tool for various tasks in SQL Server. I can’t start the agent from here, as the underlying implementation is different, and I’m not really a host OS admin when connecting in SSMS.

    2018-05-23 15_19_26-SQLQuery4.sql - ubuntu.master (sa (56))_ - Microsoft SQL Server Management Studi

    After checking which patch level I was at (CU6), I changed to my Linux console, and ran the configuration utility. For Linux, this is mssql-conf.

    2018-05-23 15_20_34-Ubuntu 64-bit SQL Server .210 - VMware Workstation

    The next step is to run the command listed to restart the system. I actually just ran

    sudo sysmctl restart mssql-server

    Once this was done, I refreshed SSMS.

    2018-05-23 15_22_52-SQLQuery4.sql - ubuntu.master (sa (56))_ - Microsoft SQL Server Management Studi

    This appears to work, but let’s test it. I’ll create a new job that does a backup of a database. This should be simple, and I’ll use defaults, just a filename for a full backup. I’ll use this command:

    backup database dbaadmin to disk = ‘dbaadmin.bak’ with init

    I save the job and run it. Sure enough, I have a backup.

    2018-05-23 15_28_37-Ubuntu 64-bit SQL Server .210 - VMware Workstation

    Tada, now I can move forward with work.

    SQLNewBlogger

    This is a fairly simple thing to do, but the writing helps me remember, but more importantly, I can document that I’ve done some learning here and experimenting.

    The next person thinking about interviewing you wants to know that you can learn and solve problems.

  • Finding Tabs in SSMS–#SQLNewBlogger

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

    Someone posted this as a question and I thought it was worth noting. I use SQL Prompt for formatting, and never worry if there are tabs in code, but I know there are people without this amazing tool.

    I added some tabs to a script and want to replace them.

    2018-05-04 09_50_15-SQLQuery1.sql - (local)_SQL2016.sandbox (PLATO_Steve (60))_ - Microsoft SQL Serv

    I hit CTRL+H for the search and replace toolbar. The tab character is represented by a \t in a regular expression. I enter that, and then enter 4 spaces in the replace text box. I do need to click the “Use Regular Expressions” button, which is the one my arrow is on in the image below. It’s a little box with an asterisk in it

    2018-05-04 09_51_39-SQLQuery1.sql - (local)_SQL2016.sandbox (PLATO_Steve (60))_ - Microsoft SQL Serv

    When I do that, tabs are highlighted in SSMS.

    2018-05-04 09_53_32-SQLQuery1.sql - (local)_SQL2016.sandbox (PLATO_Steve (60))_ - Microsoft SQL Serv

    If I click “replace all”, I see 4 replacements, and if I check, the tabs are gone and spaces live.

    2018-05-04 09_51_48-SQLQuery1.sql - (local)_SQL2016.sandbox (PLATO_Steve (60))_ - Microsoft SQL Serv

    SQLNewBlogger

    This is a quick post, an example of what you can do to show you’re building better work habits and learning about your tools.

  • 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