DBMS day 3

유요한·2022년 4월 13일
0

DBMS

목록 보기
3/8

DML(Data Manipulation Language)

  • 데이터 조작어
  • 실질적으로 데이터들을 CRUD 작업을 하는 언어

데이터 추가

  • 방법 1
    insert into 테이블명[(컬럼명1, 컬럼명2, ...)]
    values(값1, 값2, ...);
	# table car에 있는 carnum, brand, color만 추가
    # null이 들어간다.
	insert into car (carnum, brand, color)
	values('123가4567','Ferrari','Red');
	# foreign key는 비어있게 넣을 수는 있지만, 참조값이 없는 경우에는 삽입 불가능
    # insert into car
	# values('134다7961', 'k8', 'White', 4000000, 'banana');
	# → 여기서 보면 마지막에 'banana'가 들어가 있는데 table car을 보면 마지막은 id(FK)다.
    # → 이 id(FK)는 부모 테이블인 owner에 id = 'apple'에서 가져온 것이므로
    # → 'banana'를 쓰면 참조값이 없어서 삽입이 불가능하다.
    
	insert into car
	values('135다7961', 'k8', 'White', 4000000, 'apple');
  • 방법 2
    insert into 테이블명[(컬럼명1, 컬럼명2, ...)] values (값1, 값2, ...);
insert into car values ('246다2468', 'Porsche', 'Yellow', 180000000, 'apple');

데이터 삭제

  • delete from 테이블명 where 조건식
	delete from car where carnum = '135다7961';

데이터 수정

  • update 테이블명 set 컬럼명 = 새로운 값 where 조건식;
    # null 비교는 is(컬럼 is null / 컬럼 is not null)
	# 조건이 null인 table car에 있는 id를 'apple'로 변경
    update car set id = 'apple'  where id is null;
    # 조건이 '123가4567'인 table car에 있는 price를 650000000으로 변경
	update car set price = 650000000 where carnum = '123가4567';

데이터 검색(★)

  • 방법 1
    select 컬럼명1, 컬럼명2, ... from 테이블명 where 조건식;
	# 검색된 결과가 하나의 표다.
    select price from car where carnum = '123가4567';
  • 방법 2
    select from 테이블명
    select
    from 테이블명 where 조건식
    	# table car 모두 불러옴 
    	select * from car;
       # 조건식이 가격이 1억이상인 table car을 불러옴
       select * from car where price > 100000000;
		○ select * from 테이블명 where 조건식 or 조건식
 ```sql
  	  select * from car where brand = 'Ferrari' or brand = 'Porsche';
      # 컬럼 in (값1, 값2, ...) : 컬럼에 존재하는 값이 뒤
	  select * from car where brand in('Ferrari', 'Porsche');
	○ select * from 테이블명 where 조건식 and 조건식
	select * from car where price >= 100000000 and price <= 200000000;
	# 컬럼 between 값1 and 값2 : 컬럼의 값이 값1 이상 값2 이하 이면 참
	select * from car where price between 100000000 and 200000000;
■ 여기서 *가 나오는데 *은 '모든'이라는 뜻
	# table country에 있는 모든 것을 검색
	select * from country;

별칭정하기

  • 검색할 때 편하기위해서 별칭을 정할 수 있다.
  • select 컬럼명1 별칭, 컬럼명2 별칭 from 테이블명 where 조건식
	# 별칭(ALIAS)
	# 원래는 as를 써야하는데 생략 가능!
	select carnum as 차번호, price 가격 from car where price > 100000000;
  • 별칭이 키워드거나 띄어쓰기가 포함된 경우 쌍따음표로 묶어준다.
	select 1+1+12 *364/112 "계산된 결과" from dual;
    # dual : 간단한 값이나 연산의 결과를 검색하기 위한 한 행짜리 내장 테이블

조건식

where 조건식

  • = : 같다.
  • !=, <> : 다르다.
  • and : 두 조건식이 모두 참이면 참
  • or : 두 조건식이 하나라도 참이면 참

데이터 추가, 수정, 삭제, 검색 연습

	#☆★☆★☆★☆★			database : web0315			☆★☆★☆★☆★☆★☆★
use web0315;
# owner에 두명 더 추가하기(banana, cherry)
insert into owner values('banana','바나나', 01012345678, 22, '서울시 구로구');
insert into owner values('cherry', '체리', 01098245568, 24, '서울시 양천구');
# car에 3대 더 추가하기(cherry의 2000만원 이하짜리 필수, banana의 4000이상 1억이하 필수)
insert into car values('111다1111', 'k8', 'Black', 4000000, 'banana');
insert into car values('222가2222', 'genesis','Black',90000000,'banana');
insert into car values('333나3333', 'tico', 'Red', 3000000,'cherry'); 
# car에서 banana의 자동차 중 4000만원 이상 1억원 이하의 자동차 색깔을 Gold로 바꾸기
update car set color = 'Gold' where id = 'banana' and price between 40000000 and 100000000;
# car에서 cherry 자동차 중 2000만원 이하의 자동차 삭제하기
delete from car where id = 'cherry' and price <= 20000000;

select * from owner;

select * from car;

#☆★☆★☆★☆★			database : world			☆★☆★☆★☆★☆★☆★
use world;
#country 테이블
#소속 대륙(Continent)이 Asia인 나라 검색
select * from country where Continent = 'Asia';  

#소속 대륙(Continent)이 Europe이 아닌 나라 검색
select * from country where Continent != 'Europe';

#인구수(Population)가 1000만 이하인 나라 검색
select * from country where Population <= 10000000;

#인구수(Population)가 7000만 이상인 나라 검색
select * from country where Population >= 70000000;

#인구수(Population)는 4500만 이상이면서 넓이(SurfaceArea)가 10만제곱km이하인 나라 검색
select* from country where Population >= 45000000 and SurfaceArea <= 100000;

#소속 대륙(Continent)은 Asia 이고 건국 연도(IndepYear)가 1948인 나라 검색
select * from country where Continent = 'Asia' and IndepYear = 1948;

#지역(Region)이 동아프리카(Eastern Africa) 이고 건국 연도 가 1970~1980인 나라 검색
select * from country where Region = 'Eastern Africa' and IndepYear between 1970 and 1980;

profile
최선을 다하자!!

0개의 댓글