Skip to main content

SQL Server GO command and Column Nullability

GO command

You can write sql statements/commands using tools such as SQL Server Management Studio Code Editor and sqlcmd. Then the sql code is executed by SQL Server. Above tools merely send your sql code to the SQL server to be executed.

SQL Server does NOT use GO command. It is never sent to the server. Rather, GO command is used by SQL Server Management Studio Code Editor, sqlcmd and osql utilities.

GO signals the end of a batch of Transact-SQL statements to the SQL Server utilities. source

What GO command do is to instruct the sqlcmd, osql, SQL Server Management Studio Code Editor etc. to send the code up until the GO command as one batch. Basically it is a code (batch) separator. The SQL Server utilities never send a GO command to the server.

Why and When GO command is needed

Before executing the sql code, SQL server validates it. If, for example, (single batch of) sql code includes both CREATE and ALTER statements, when validating ALTER statement, sql server raises error. This is because when ALTER statements is being validated, the table does not exist yet. SQL server thinks ALTER statement is referencing undefined table and raises error.

Below code threw error when ran with sqlcmd:

CREATE TABLE my_table (
ID int IDENTITY(1,1) NOT NULL,
column1 VARCHAR(10) NOT NULL,
column2 DECIMAL(3,0) NOT NULL,
);

ALTER TABLE my_table
ADD column3 DATETIME NULL;

To avoid error, we should execute CREATE and ALTER statements separately.

CREATE TABLE my_table (
ID int IDENTITY(1,1) NOT NULL,
column1 VARCHAR(10) NOT NULL,
column2 DECIMAL(3,0) NOT NULL,
);
-- ↑↑↑ Batch 1
GO
--  ↓↓↓↓ Batch 2
ALTER TABLE my_table
ADD column3 DATETIME NULL;

Nullability

While creating or altering the table, if you leave column nullability constraint unspecified ( whether the column can allow a null value or not), that column might end up with NOT NULL or NULL (nullable) data type depending on the database and session settings.

When you use CREATE TABLE or ALTER TABLE to create or alter a table, database and session settings influence and may override the nullability of the data type that is used in a column definition. Sometimes column left blank might be NOT NULL or NULL (nullable). source

Not defining column nullability explicitly leads to unexpected behaviors. For example, all columns defined within a PRIMARY KEY constraints must be defined as NOT NULL. If columns whose data types are nullable due to not being explicitly defined are used within PRIMARY KEY constraints, SQL Server raises error.

Below columns might end up either with NOT NULL or NULL (nullable) data type.

CREATE TABLE my_table (
column1 VARCHAR(10),
column2 DECIMAL(3,0),
);

To avoid unexpected behavior, it is recommended to explicitly define a column as NULL or NOT NULL.

CREATE TABLE my_table (
column1 VARCHAR(10) NOT NULL,
column2 DECIMAL(3,0) NULL,
);

Comments

Popular posts from this blog

脱初心者! Git ワークフローを理解して開発効率アップ

Git – チーム開発に必須のバージョン管理システムですが、その真価を発揮するにはワークフローの理解が欠かせません。 色々な人は Git の使い方を良く知っていますが、Git を仕事やワークフローに統合する方法を余り良く知らない人もいます。本記事では、Git をワークフローに組み込むことで、開発プロセスがどのように効率化され、チーム全体のパフォーマンスが向上するのかを解説します。Centralized Workflow から Forking Workflow まで、代表的な 9 つのワークフローの特徴を分かりやすく紹介します。それぞれのメリット・デメリット、そして最適なユースケースを理解することで、あなたのプロジェクトに最適なワークフローを選択し、開発をスムーズに進めましょう! Centralized Workflow Feature branching/GitHub Flow Trunk Based Flow Git Feature Flow Git Flow Enhanced Git Flow One Flow GitLab Flow Forking Workflow 分かりやすくするために、同じコンセプトを説明するに一つ以上の図を使った場合があります。 Centralized Workflow 説明: 集中化ワークフローではプロジェクトにおけるすべての変更の単一の入力箇所として中央リポジトリを使用します。デフォルトの開発用ブランチは main と呼ばれ、すべての変更がこのブランチにコミットされます。 集中化ワークフローでは main 以外のブランチは不要です。チームメンバー全員がひとつのブランチで作業し、変更を直接中央リポジトリにプッシュします。 メリット: SVN のような集中型バージョン管理システムから移行する小規模チームに最適。 デメリット: お互いのコードが邪魔になり (お互いの変更を上書きするように)、プロダクション環境にバグをい入れる可能性が高くて、複数のメンバいるチームでこのフローを使いにくい。 地図: graph TD; A[Central Repository] -->|Clone| B1[Developer A's Local Repo] A --...

Introduction to SQLFluff: How to make your SQL code clean and error-free

Image by Jake Aldridge from Pixabay You know oftentimes, the cause of runtime or compile errors and hours of debugging agony is all due to simply a missing semicolon. Have you ever had such experience? If you had, you are not alone. There are two ways to avoid these unfortunate situations: either become a perfect developer who never makes mistakes, or use helpful tools such as linters that can catch these errors early on. I am nowhere near being a perfect developer who never makes a mistake. In fact, I'm probably the opposite of a perfect developer, so even if I wanted to, I wouldn’t be able to teach you how to become a perfect developer. But what I can teach you is using linters. A Wikipedia defines a linter as a "static code analysis tool used to flag programming errors, bugs, stylistic errors and suspicious constructs." If you're not convinced yet on using linters, consider this scenario: in a large project with multiple members, different people tend to ...

WinMerge のセットアップと使う方

WinMerge は、Windows 用のオープン ソースの差分およびマージ ツールです。WinMerge は、フォルダーとファイルの両方を比較し、違いを理解して扱いやすい視覚的なテキスト形式で表示します。この記事でWinMerge のセットアップと使う方を教えます。 source: https://winmerge.org WinMerge をダウンロード WinMerge のウェブサイト に行って、「WinMerge-2.16.44-x64-Setup.exe」ボタンを押し、WinMerge 2.16 をダウンロードしてください。 WinMerge をインストール ダウンロードされたソフトウェアをクリックし、ポップアップ画面で「Next」を押してください 「Languages」部分をスクロールダウンし、「Japanese menus and dialogs」を選択し、「Next」ボタンを押してください ターミナル等からも WinMerge をアクセス出来ようにする為に「Add WinMerge folder to your system path」オプションを選択し、希望によって他のオプション選択してください 「Enable Explorer context menu Integration」オプションを選択したら、フォルダ/ファイルを右キリックし、コンテクストメニューから WinMerge を開くようになります。 「Install」ボタンを押し、「Next」ボタンを押し、その後、「Finish」ボタンを押してください 言語を日本語にする もし WinMerge の言語が日本語じゃなくて、英語なら、「Edit」タブから「Options」を押してください。 ポップアップ画面で右側の下にある「Languages」と言うドロップダウンメニューから日本語を選択し、「OK」ボタンを押してください WinMerge を使う方 「ファイル」タッブから「開く」を押し 参照ボタンを押し、比較したいフォルダ・ファイルを指定 比較したいフォルダを指定する方法: ポップアップ画面から対象のフォルダーを選択し、「Open」を押してくだい 何も選択しないで、「Open」を押してくだい 右側下にある「比較」ボタンを押し ...