0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SQLServerメモ

Last updated at Posted at 2024-02-16

はじめに

SQLServerのDDLについてのメモです。

DDL

テーブル定義確認

EXEC sp_help 'dbo.テーブル名';

カラム定義

-- カラム追加
ALTER TABLE テーブル名 ADD カラム名 データ型 制約;

-- カラム変更
ALTER TABLE テーブル名 ALTER COLUMN カラム名 データ型 制約;

-- カラム削除
ALTER TABLE テーブル名 DROP COLUMN カラム名;

デフォルト値定義確認

SELECT
	sdc.name,
	s.name + '.' + t.name
FROM
	sys.default_constraints sdc
INNER JOIN sys.tables t
	ON t.object_id = sdc.parent_object_id
INNER JOIN sys.schemas s
	ON s.schema_id = t.schema_id
WHERE
    t.name = 'テーブル名'
	--AND sdc.name = 'デフォルト値定義名'
ORDER BY
	sdc.name

デフォルト値定義

-- 定義削除
ALTER TABLE テーブル名 DROP CONSTRAINT デフォルト値定義名;

-- 定義作成
ALTER TABLE テーブル名 ADD CONSTRAINT デフォルト値定義名 DEFAULT 制約 FOR カラム名;

インデックス定義確認

SELECT
    indx.name    -- インデックス名
    , clmns.name -- インデックスのカラム名
FROM sys.index_columns AS indx_clmns
JOIN sys.indexes AS indx 
    ON indx_clmns.object_id = indx.object_id AND indx.index_id = indx_clmns.index_id
JOIN sys.columns AS clmns
    ON indx.object_id = clmns.object_id AND indx_clmns.column_id = clmns.column_id
WHERE indx.object_id = OBJECT_ID('dbo.テーブル名')

シーケンス定義確認

SELECT * FROM sys.sequences;

シーケンス定義

CREATE SEQUENCE [schema_name . ] sequence_name  
    [ AS [ built_in_integer_type | user-defined_integer_type ] ]  
    [ START WITH <constant> ]  
    [ INCREMENT BY <constant> ]  
    [ { MINVALUE [ <constant> ] } | { NO MINVALUE } ]  
    [ { MAXVALUE [ <constant> ] } | { NO MAXVALUE } ]  
    [ CYCLE | { NO CYCLE } ]  
    [ { CACHE [ <constant> ] } | { NO CACHE } ]  
    [ ; ]  

シーケンス値を採番して取得

SELECT NEXT VALUE FOR シーケンス名

References

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?