Skip to content

Instantly share code, notes, and snippets.

@dungdm93
Created February 11, 2020 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dungdm93/4fd6612c9408b65612f5beb1e0ca8da7 to your computer and use it in GitHub Desktop.
Save dungdm93/4fd6612c9408b65612f5beb1e0ca8da7 to your computer and use it in GitHub Desktop.
-- Show all databases
SELECT
database_id,
name,
create_date
FROM sys.databases;
--or using Stored Procedure
EXEC sp_databases;
-- Switch database
USE AdventureWorks;
-- Check current db name
SELECT DB_NAME();
-- List all tables of current db
SELECT
TABLE_NAME,
TABLE_SCHEMA
FROM INFORMATION_SCHEMA.TABLES;
-- List all columns of given table
SELECT
ORDINAL_POSITION,
TABLE_NAME,
COLUMN_NAME,
DATA_TYPE,
CHARACTER_MAXIMUM_LENGTH,
IS_NULLABLE,
COLUMN_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Product';
SELECT CONSTRAINT_NAME, TABLE_NAME, TABLE_SCHEMA, CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA
FROM INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE
WHERE TABLE_NAME = 'Product';
sp_help 'SalesLT.Product';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment