'Database'에 해당되는 글 11건

  1. 2022.08.17 오라클 버전 확인 쿼리
  2. 2022.03.07 SQLite 개요
  3. 2021.04.01 subtotal,cube,grouping_id
  4. 2019.12.29 [Oracle] HR계정 사용하기
Database/Oracle2022. 8. 17. 08:41
728x90
SELECT * 
  FROM v$version
;

 

728x90
Posted by 하루y
Database/SQLite2022. 3. 7. 22:15
728x90

SQLite 링크: https://www.sqlite.org/index.html

 

Python SQLite : https://docs.python.org/ko/3/library/sqlite3.html

 

- 내장 가능한 오픈소스 데이터베이스이다. C로 작성됐으며 일반적인 SQL로 쿼리가 가능한다.

- SQL은 다양한 플랫폼에서 이식됨.

 

1. 장점

- 관계형 데이터베이스 역할을 하고, 트랜잭션을 지원함.

- 전체 텍스트 인덱스, JSON 데이터 지원 등 고급 데이터베이스 기능을 지원함.

- 단일 독립형 바이너리이므로 앱과 함께 배포하고 앱과 이동하기 쉽다.

 

2. 특징

- 데이터형식은 TEXT, NUMERIC, INTEGER, REAL, BLOB 로 소수다. 일자는 함수를 제공한다.

   https://www.sqlite.org/lang_datefunc.html

- SQLite는 단일 애플리케이션이 적합하고, 동시 사용자는 MySQL 또는 내장DB중에는 버클리DB가 적당함.

  버클리DB 엔진 성능은 SQLite를 못 따라감.

 

9. 참조

9.1 isolation_level : https://www.sqlite.org/lang_transaction.html

 - None : autommit
 - 'DEFERRED': (default) 쓰기모드에서 트랜잭션 시작됨. commit / rollback 처리 필요.
 - 'IMMEDIATE': 데이터베이스 연결 즉시 트랜잭션 시작.
 - 'EXCLUSIVE': IMMEDIATE와 유사하지만 트랜잭션 동안 다른 데이터베이스 연결이 데이터베이스 읽는 것을 방지함.

9.2 다중 스레드 응용 프로그램에서 SQLite 사용

 - 관련글: https://docs.python.org/ko/3/library/sqlite3.html (sqlite3.threadsafety)

             https://www.sqlite.org/threadsafe.html

 - python / sqlite3 에서 qlite3.threadsafety=1 로 스레드는 모듈을 공유할 수 있지만, 연결을 공유할 수 없음.

   연결 시 check_same_thread=True(기본값)를 False로 변경하면, 다른 스레드로 공유가능함.

   단, 쓰기에서는 직렬화 해서 사용해야 안전한데 현재 sqlite3에서는 지원이 안되는 것 같음.

  • Python Sample Code
import sqlite3

# 실행위치에 파일이 생성되고, 상대위치를 지정하면 실행 최상위를 계산해서 수행함.
conn = sqlite3.connect('./universe_price.db', isolation_level=None)
cur = conn.cursor()

def create_table():
  cur.execute('''CREATE TABLE IF NOT EXISTS 
  BALANCE
  (
    CODE VARCHAR(6) PRIMARY KEY,
    BID_PRICE INT(20) NOT NULL,
    QUANTITY INT(20) NOT NULL,
    CREATED_AT VHARCHAR(14) NOT NULL,
    WILL_CLEAR_AT VARCHAR(14)
  )
  ''')

# 단건 등록 테스트
def test_insert_row():
  sql = """INSERT INTO BALANCE(CODE, BID_PRICE, QUANTITY, CREATED_AT, WILL_CLEAR_AT) VALUES
  (?, ?, ?, ?, ?)
  """
  cur.execute(sql, ('005930', 70000, 10, '20201222', 'today'))
  # conn.commit()
  print(cur.rowcount)

# 복수건 등록 테스트
def test_insert_rows():
  sql = """INSERT INTO BALANCE(CODE, BID_PRICE, QUANTITY, CREATED_AT, WILL_CLEAR_AT) VALUES
  (?, ?, ?, ?, ?)
  """
  stocks = [
    ('010060', 109500, 10, '20201222', 'today'),
    ('005380', 168000, 10, '20201222', 'today'),
    ('336260', 39700, 10, '20201222', 'today')
  ]
  cur.executemany(sql, stocks)
  # conn.commit()
  print('insert row count: {}'.format(cur.rowcount))

# 단건 조회 테스트
def test_select_one():
  cur.execute('SELECT * FROM BALANCE')
  row = cur.fetchone()
  print(row)

# 전체 조회 테스트
def test_select_all():
  cur.execute('SELECT * FROM BALANCE WHERE BID_PRICE > :BID_PRICE', {'BID_PRICE':50000})
  rows = cur.fetchall()
  print('# 전체 조회 테스트')
  for row in rows:
    code, bid_price, quantity, created_at, will_clear_at = row
    print(row)

# 일부건 패치 테스트
def test_select_many(row_count):
  cur.execute('SELECT * FROM BALANCE')
  rows = cur.fetchmany(row_count)
  print('# 일부건 조회 테스트')
  for row in rows:
    code, bid_price, quantity, created_at, will_clear_at = row
    print(row)

def close_conn():
  conn.close()

if __name__ == "__main__":
  create_table()
  # test_insert_row()
  # test_insert_rows()
  test_select_all()
  test_select_many(2)

close_conn()

end.

 

728x90
Posted by 하루y
Database/Oracle2021. 4. 1. 00:42
728x90

1. 참조사이트: https://oracle-base.com/articles/misc/rollup-cube-grouping-functions-and-grouping-sets

 

2. Oracle 
 - URL : https://livesql.oracle.com/apex/f?p=590:1:107662273582818::NO:::

 

 

select department_id, gubun, gubun2, sum(salary) as sum_salary
  from employees
 group by department_id, cube(gubun, gubun2)
 order by 1, 2, 3
; 

-- subtotal  
select department_id, gubun, gubun2, sum(salary) as sum_salary, grouping(gubun) as f1, grouping(gubun2) as f2,  grouping_id(department_id, gubun, gubun2) as g1
  from employees
 group by cube(department_id, gubun, gubun2)
 having grouping_id(department_id, gubun, gubun2) not in (1, 3, 4, 5, 7)
 order by 1, 2, 3
; 

-- subtotal
with tb_a as (
select decode(grouping_id(department_id, gubun, gubun2), 6, 'total:', department_id) as department_id
     , decode(grouping_id(department_id, gubun, gubun2), 2, 'subtotal:'|| department_id, gubun) as gubun
     , gubun2
     , sum(salary) as sum_salary
     , grouping(gubun) as f1
     , grouping(gubun2) as f2
     , grouping_id(department_id, gubun, gubun2) as g1
  from employees
 group by cube(department_id, gubun, gubun2)
 having grouping_id(department_id, gubun, gubun2) not in (1, 3, 4, 5, 7)
 order by 1, 2, 3
)
select *
  from tb_a a
 where g1 = 6
union all
select *
  from tb_a a
 where g1 != 6
; 

DEPARTMENT_ID	GUBUN	GUBUN2	SUM_SALARY	F1	F2	G1
total:	 - 	하위1	33200	1	0	6
total:	 - 	하위2	42200	1	0	6
total:	 - 	하위3	10100	1	0	6
total:	 - 	하위4	20000	1	0	6
10	subtotal:10	하위1	30000	1	0	2
10	subtotal:10	하위2	40000	1	0	2
10	subtotal:10	하위3	10000	1	0	2
10	구분1	하위1	20000	0	0	0
10	구분1	하위2	20000	0	0	0
10	구분2	하위1	10000	0	0	0
10	구분2	하위2	20000	0	0	0
10	구분2	하위3	10000	0	0	0
20	subtotal:20	하위4	20000	1	0	2
20	구분1	하위4	20000	0	0	0
30	subtotal:30	하위1	3000	1	0	2
30	구분B	하위1	3000	0	0	0
40	subtotal:40	하위2	2000	1	0	2
40	구분B	하위2	2000	0	0	0
50	subtotal:50	하위1	200	1	0	2
50	subtotal:50	하위2	200	1	0	2
50	subtotal:50	하위3	100	1	0	2
50	구분1	하위2	200	0	0	0
50	구분3	하위1	200	0	0	0
50	구분3	하위3	100	0	0	0

  
select department_id, gubun, gubun2, sum(salary) as sum_salary
     --, grouping(gubun) as f1
     --, grouping(gubun2) as f2
     , grouping_id(department_id, gubun, gubun2) as grouping_id
     , group_id() as group_id
  from employees
 group by grouping sets(department_id, cube(department_id, gubun, gubun2))
 -- having grouping_id(department_id, gubun, gubun2) not in (1, 3, 4, 5, 7)
 order by 1, 2, 3
; 
  
728x90
Posted by 하루y
Database/Oracle2019. 12. 29. 06:41
728x90

오라클 HR계정 Unlock 하기. 또는 비번이 기억나지 않을 때.

 

1. sqlplus로 system계정으로 로그인 한다.

 

2. 아래와 같이 unlock하고 비번을 변경한다.

  SQL> alter user hr account unlock;

  SQL> alter user hr identified by 비번;

 

end.

728x90
Posted by 하루y