Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
leandromonaco committed Sep 9, 2024
1 parent 31129f9 commit 7a93590
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 167 deletions.
2 changes: 1 addition & 1 deletion .obsidian/bookmarks.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{
"type": "file",
"ctime": 1696310935338,
"path": "articles/Tools/SQL Lite.md"
"path": "articles/Database/SQL Lite.md"
}
]
}
43 changes: 28 additions & 15 deletions .obsidian/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,26 @@
"state": {
"type": "markdown",
"state": {
"file": "articles/Engineering Management/Engineering Management.md",
"file": "articles/Development/Development Environment Setup.md",
"mode": "source",
"source": false
}
}
},
{
"id": "10b6c6dba9453fac",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "articles/Database/Useful SQL Server Queries.md",
"mode": "source",
"source": false
}
}
}
]
],
"currentTab": 1
}
],
"direction": "vertical"
Expand Down Expand Up @@ -85,7 +98,7 @@
"state": {
"type": "outgoing-link",
"state": {
"file": "articles/Engineering Management/Engineering Management.md",
"file": "articles/Database/Useful SQL Server Queries.md",
"linksCollapsed": false,
"unlinkedCollapsed": true
}
Expand All @@ -108,7 +121,7 @@
"state": {
"type": "outline",
"state": {
"file": "articles/Engineering Management/Engineering Management.md"
"file": "articles/Database/Useful SQL Server Queries.md"
}
}
},
Expand Down Expand Up @@ -138,7 +151,7 @@
"state": {
"type": "backlink",
"state": {
"file": "articles/Engineering Management/Engineering Management.md",
"file": "articles/Database/Useful SQL Server Queries.md",
"collapseAll": false,
"extraContext": false,
"sortOrder": "alphabetical",
Expand All @@ -150,7 +163,7 @@
}
}
],
"currentTab": 2
"currentTab": 1
}
],
"direction": "horizontal",
Expand All @@ -168,10 +181,17 @@
"webpage-html-export:Export Vault to HTML": false
}
},
"active": "8926ef53c314d9bc",
"active": "10b6c6dba9453fac",
"lastOpenFiles": [
"articles/Engineering Management/Index.md",
"articles/Database/Sql Server.md",
"articles/Database/SQL Lite.md",
"articles/Development/Development Environment Setup.md",
"articles/Database/Useful SQL Server Queries.md",
"articles/Database",
"articles/DotNet/ASP.NET.md",
"articles/Engineering Management/Engineering Management.md",
"docs/CNAME",
"articles/Engineering Management/Index.md",
"articles/Engineering Management/Research.md",
"Cloud Costs.md",
"articles/How-to/Git - Update dev branch with latest main branch.md",
Expand All @@ -186,8 +206,6 @@
"docs/docs/src/how-to/measure-method-elapsed-time-with-csharp.html",
"docs/docs/src/how-to/change-host-file-on-windows.html",
"docs/docs/src/how-to/install-multiple-nodejs-versions.html",
"docs/docs/src/how-to/http-client.html",
"docs/docs/src/how-to/git.html",
"docs/docs/images/what-engineering-managers-should-do.png",
"docs/docs/images/pasted-image-20240729081305.png",
"docs/docs/images/pasted-image-20240510085959.png",
Expand All @@ -209,11 +227,6 @@
"src/Tools/Powershell.md",
"src/Tools/Nuget CLI.md",
"src/Tools/NuGet.md",
"src/Tools/Microsoft Application Inspector.md",
"src/Tools/Kubernetes.md",
"src/Tools/InferSharp.md",
"src/Tools/Ideas.md",
"src/Tools/GitHub Copilot.md",
"images/Pasted image 20230731083648.png",
"images/Pasted image 20240510085959.png",
"Untitled.canvas",
Expand Down
File renamed without changes.
153 changes: 2 additions & 151 deletions articles/Tools/Sql Server.md → articles/Database/Sql Server.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,161 +62,12 @@ winget upgrade -e --id Microsoft.Sqlcmd
Enable transient error resiliency by adding `EnableRetryOnFailure` to the `UseSqlServer` call on [[ASP.NET]]

## Useful Commands

`sp_helptext`
## Useful Queries
### Backup
```sql

--Windows
BACKUP DATABASE [DbName] TO
DISK = N'C:\Dev\Database\DbName.bak'
WITH COMPRESSION, STATS = 10
GO

--Linux
BACKUP DATABASE [DbName] TO
DISK = N'/var/opt/mssql/data/DbName.bak'
WITH COMPRESSION, STATS = 10
GO
```
### Paging
```sql
--https://learn.microsoft.com/en-us/sql/t-sql/queries/select-order-by-clause-transact-sql
SELECT *
FROM Employee
ORDER BY EmployeeId
OFFSET 20 ROWS
FETCH NEXT 5 ROWS ONLY
```
### Restore
```sql
USE master;
GO

ALTER DATABASE [DbName]
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
GO

RESTORE DATABASE [DbName]
FROM DISK = N'C:\Dev\Database\DbName.bak'
WITH RECOVERY
GO

RESTORE DATABASE [DbName]
FROM DISK = N'C:\Dev\Database\DbName.bak'
WITH MOVE 'DbName' TO 'C:\Dev\Database\DbName.mdf',
MOVE 'DbName_log' TO 'C:\Dev\Database\DbName_log.ldf',
RECOVERY, REPLACE
GO

RESTORE DATABASE Local
FROM DISK = N'/var/opt/mssql/data/Local.bak'
WITH MOVE 'Database' TO '/var/opt/mssql/data/Local.mdf',
MOVE 'Database_Log' TO '/var/opt/mssql/log/Local.ldf',
RECOVERY, REPLACE
GO
```
### Get all linked tables by FK
```sql
SELECT
OBJECT_NAME(fkeys.constraint_object_id) foreign_key_name,
OBJECT_NAME(fkeys.parent_object_id) referencing_table_name,
COL_NAME(fkeys.parent_object_id, fkeys.parent_column_id) referencing_column_name,
OBJECT_SCHEMA_NAME(fkeys.parent_object_id) referencing_schema_name,
OBJECT_NAME (fkeys.referenced_object_id) referenced_table_name,
COL_NAME(
fkeys.referenced_object_id,
fkeys.referenced_column_id
) referenced_column_name,
OBJECT_SCHEMA_NAME(fkeys.referenced_object_id) referenced_schema_name
FROM
sys.foreign_key_columns AS fkeys
ORDER BY referenced_table_name
```
### View the compatibility level of a database
```sql
USE AdventureWorks2019;
GO
SELECT compatibility_level
FROM sys.databases WHERE name = 'AdventureWorks2019';
GO
```
### List all tables that are linked by foreign keys
```sql
SELECT
FK_Table = FK.TABLE_NAME,
FK_Column = CU.COLUMN_NAME,
PK_Table = PK.TABLE_NAME,
PK_Column = PT.COLUMN_NAME,
Constraint_Name = C.CONSTRAINT_NAME
FROM
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
INNER JOIN
INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK
ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
INNER JOIN
INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK
ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU
ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
INNER JOIN
(
SELECT
i1.TABLE_NAME,
i2.COLUMN_NAME
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2
ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
WHERE
i1.CONSTRAINT_TYPE = 'PRIMARY KEY'
) PT
ON PT.TABLE_NAME = PK.TABLE_NAME
```

### List tables with no records
```sql
SELECT
t.NAME AS TableName,
p.rows AS RowCounts
FROM
sys.tables t
INNER JOIN sys.partitions p ON t.object_id = p.OBJECT_ID
WHERE
t.NAME NOT LIKE 'dt%'
AND t.is_ms_shipped = 0
AND p.rows = 0
GROUP BY
t.Name,
p.Rows
ORDER BY
t.Name
```
[[Useful SQL Server Queries]]

### List all DB objects in a Database
```sql
SELECT name,*
FROM [DBName].sys.all_objects
WHERE upper(name) LIKE upper('%someName%')
ORDER BY type_desc
```

## Data Size

```sql
SELECT CAST(SUM(TotalSize) AS varchar(255)) + ' bytes'
FROM(
SELECT (DATALENGTH(Column) +
DATALENGTH(Column) +
DATALENGTH(Column) +
DATALENGTH(Column) +
DATALENGTH(Column)) AS TotalSize
FROM TableName
) t
```
## Encryption by Certificate
- https://docs.microsoft.com/en-us/sql/t-sql/functions/encryptbycert-transact-sql
- https://docs.microsoft.com/en-us/sql/t-sql/functions/decryptbycert-transact-sql
Expand Down
Loading

0 comments on commit 7a93590

Please sign in to comment.