SQL정리

세미·2022년 10월 25일
0

데이터베이스

목록 보기
3/9

1. Create Table Construct

create table 테이블(
	ID			char(5), #fixed length
    name 		varchar(20), #maximum length
    dept_name 	varchar(20),
    salery		numeric(8,2) #8:길이 2:소수점
)

Integrity Constraints in Create Table

  • primary key(a1,a2...)
  • foreign key(a1,a2...) references r
  • not null
create table 테이블(
	ID			char(5), 
    name 		varchar(20) not null, -> 이름을 비워둔채 update하면 reject 
    dept_name 	varchar(20),
    salery		numeric(8,2) #8:길이 2:소수점
    
    primary key (ID),
    foreign key (dept_name) reference department;
)
  • 외래키
    • 다른 릴레이션의 기본키를 참조하는 속성.
    • 다른 릴레이션의 기본키를 참조하여 관계 데이터 모델의 특징인 릴레이션 간의 관계를 표현. (+) 관계데이터모델에서 설명

Updates to tables

  1. insert
insert into 테이블 values('a','b','c');
  1. delete
delete from 테이블 #모든 튜플 삭제. table은 생존.
  • drop table
drop table 테이블. #table까지 삭제
  • alter
alter table r add 속성 속성값(domain)

alter table r drop 속성

Query Structure

SELECT 속성 (A1,A2..)
FROM 테이블
     [INNER JOIN , LEFT(OUTER) JOIN, RIGHT(OUTER) JOIN {테이블 ON 검색조건}]
WHERE 검색조건
[GROUP BY 속성]
[HAVING 검색조건]
[ORDERED BY 속성]
  1. SELECT:
1.
select distinct 속성 #중복삭제
from 테이블

2.
select all 속성 #중복허용.

3.
select ID,name, salary/12 # 사칙연산 가능

4.
select ID,name, salary/12 as monthly_salary #이름설정 가능
  1. WHERE
1. 비교
where price <= 12000

2. 범위 (between)
where price between 100 and 500

3. 집합 (in, not in)
where price in(100,200,300)

4. 패턴 (like)
where bookname like '축구'

5. null (is null, is not null)
where price is null

6. 복합조건 and or not

like와 같이쓰이는

  1. GROUP BY
  • having : group by 절에 결과에 나타난 테이블을 설정

출처

0개의 댓글