반응형

Database/SQL Server 81

SQL Server - 트랜잭션 (Transaction)

트랜잭션이란 ? 트랜잭션은 데이터베이스에 저장된 데이터의 변경 명령문의 집합이다. 트랜잭션은 단일 개념으로 취급되며 성공하던지 실패하던지 둘 중하나이다. 진행되다가 중간에 에러가 나거나 문제가 생기면 롤백이 된다. 이로써 데이터 베이스의 무결성을 유지한다. What is a Transaction ? A transaction is a group of commands that change the data stored in a database. A transaction, is treated as a single unit. A transaction ensures that, either all of the commands succeed, or none of them. If one of the commands in..

Database/SQL Server 2020.12.09

SQL Server - 에러 처리 Error handling(@@ERROR, TRY...CATCH)

SQL server 2000 - @@ERROR SQL server 2005 & later - Try...Catch 1. SQL server 2000 RAISERROR('Error Message', ErrorSeverity, ErrorState) 에러 만들기 Severity level = 16 (사용자가 수정할 수 있는 일반적인 오류를 나타냄 Indicates general errors that can be corrected by the user) 0~25 의 숫자로 심각도를 설정 가능함,,, 0~18 일반적 19~25 WITH LOG옵션이 필요함 20이상 부터는 치명적인 오류로 간주함 State = Number between 1 & 255. RAISERROR 는 0 ~ 255 정수만 사용할 수 있음. @..

Database/SQL Server 2020.12.09

SQL Server - 피벗 오퍼레이터 (PIVOT Operator)

피벗은 테이블을 효과적으로 순환하여 한 열의 고유값을 여러열로 변환하는데 사용할 수 있는 SQL server operator이다. Pivot is a sql server operator that can be used to turn unique values from one column, into multiple columns in the output, there by effectively rotating a table. Example, Select SalesAgent, Korea, US, UK from ( Select SalesAgent, SalesCountry, SalesAmount from tblProductSale ) as SourceTable PIVOT ( SUM(SalesAmount) FOR Sa..

Database/SQL Server 2020.12.09

SQL Server - 정규화, 1~3 정규형 [1NF, 2NF, 3NF]

제 1 정규형 (First Normal Form, 1NF) 1. 각 열에 들어있는 데이터가 원자성을 가지고 있어야 한다. (중복 값 없이 콤마로 분류되지 않게) The data in each column should be atomic. No multiple values, separated by comma. 2. 테이블은 반복되는 열 그룹을 가지면 안된다. The table does not contain any repeating column groups. 3. 기본키를 사용하여 각 레코드를 식별하게 한다. Identify each record uniquely using primary key. 제 2 정규형(Secound Normal Form , 2NF) 1. 모든 테이블이 1정규형의 조건을 가지고 있어야 ..

Database/SQL Server 2020.12.08

SQL Server - 데이터베이스 정규화 Database normalization

정규화란 무엇인가 ? What is Normalization 데이터베이스 정규화란 데이터 중복을 최소화하기 위해 데이터를 구성하는 과정으로 데이터의 일관성을 보장함. Database normalization is the process of organizing data to minimize data redundancy(data duplication), which in turn ensures data consistency. 데이터 중복의 문제 Problems of Data Redundancy 1. 디스크 공간 낭비 Disk Space Wastage 2. 데이터의 불일치 Data Inconsistency 3. 데이터 불러오는 쿼리가 느려진다 DML(Data Manipulation Language) querie..

Database/SQL Server 2020.12.08

SQL Server - 재귀적 공통 테이블 식 (Recursive CTE)

재귀적 CTE 예 --A simple self join Select Employee.Name as [Employee Name], IsNUll(Manager.Name, 'Super Boss') as [Manager Name] from tblEmployee Employee left join tblEmployee Manager on Employee.ManagerId = Manager.EmployeeId With EmployeesCTE (EmployeeId, Name, ManagerId, [Level]) as ( Select EmployeedId, Name, ManagerId, 1 from tblEmployee where ManagerId is null union all Select tblEmployee.Emp..

Database/SQL Server 2020.12.08

MS SQL-파생 테이블 정리 & 공통 테이블 식(CTE) in SQL Server

테이블 원본 말고 나올 수 있는 여러 가지 테이블 Views, Table Variable, 로컬 임시 테이블, 글로벌 임시 테이블을 비교해 보자 뷰 Views, Create view vWEmployeeCount as Select DeptName, DepartmentId, COUNT(*) as TotalEmployees from tblEmployee join tblDepartment on tblEmployee.DepartmentId = tblDepartment.DeptId Group by DeptName, DepartmentId Select DeptName, Total Employees from vWEmployeeCount where TotalEmployees >= 2 메모 : 뷰는 데이터베이스에 저장된다..

Database/SQL Server 2020.12.08

SQL Server - 트리거, DML(Data Manipulation Language) trigger에 대해

트리거 종류 Trigger types 1. DML(Data Manipulation Language) triggers - 뷰나 테이블 관련하여 DML문의가 발생될 때 작동 2. DDL(Data Definition Language) triggers - 서버나 데이터베이스에 DDL문의가 발생될 때 작동 3. Logon trigger - 사용자의 로그온 이벤트가 발생될 때 작동 DML triggers Types 1. After triggers INSERT, UPDATE, DELETE 등의 이벤트가 발생 되고 난 뒤에 작동. 테이블에만 작동하고 뷰에선 작동 안함. 2. Instead of triggers INSERT, UPDATE, DELETE 등의 이벤트가 발생하기 전에 작동하는 트리거. 테이블과 뷰 다 작동이..

Database/SQL Server 2020.12.05

MS SQL - 인덱스 뷰 Indexed View

기본 표준 뷰(인덱스가 안된)는 그냥 쿼리에만 존재한다. 뷰에서 데이터를 불러오려고 할 때에 데이터는 뷰가 기반한 테이블에서 불러져 온다. 그러니 뷰는 기본적으로 가상테이블과 같고 data를 가지지 않는다. 그러나 인덱스가 뷰에 만들어진다면 그때부터 이야기가 달라진다. 구체화가 되어지고 데이터를 가질 수 있다. SQL server에서는 이것을 indexed View라고 하고 Oracle에서는 Materialized view라고 한다. 인덱스 뷰 만들기 Create view vWTotalSalesByProduct with SchemaBinding as Select Name, SUM(INSULL((QuantitySold * UnitPrice), 0)) as TotalSales, COUNT_BIG(*) as ..

Database/SQL Server 2020.12.05
반응형