본문 바로가기

프로그래밍 언어

[MySQL] 多대多관계, null인 데이터를 가져오는 법 MANY : MANY 다 대 다 관계 해당 데이터에 foreign key를 연결해준다. 테이블 reviewer, series, reviews 상기 이미지처럼 생성 및 foreign key 적용 하기 데이터 입력 use yh_db; INSERT INTO series (title, released_year, genre) VALUES ('Archer', 2009, 'Animation'), ('Arrested Development', 2003, 'Comedy'), ("Bob's Burgers", 2011, 'Animation'), ('Bojack Horseman', 2014, 'Animation'), ("Breaking Bad", 2008, 'Drama'), ('Curb Your Enthusiasm', 2000.. 더보기
[MySQL] 그룹/묶음 뒤에 조건: group by..having, 실습문제 Group by뒤의 조건: having -- group by의 결과에 대해서 조건을 걸 때는 having을 사용한다. 그 뒤에 and 및 or도 사용 가능하다. group by 컬럼명 having 묶음 후의 조건 -- 각 고객별로 주문 금액 평균이 300 달러 이상인 데이터만 가져오시오. select c.first_name, c.last_name, avg(o.amount) as AVG from customers c join orders o on c.id = o.customer_id group by c.id having avg(o.amount) >= 300; -- 실습 students, papers 테이블 생성 후, INSERT INTO students (first_name) VALUES ('Caleb').. 더보기
[MySQL] 관계형 데이터베이스 합치기: join, left join MySQL에서 많은 데이터 insert 时 - 번개모양 클릭 - 처음부터 끝까지 자동으로 실행해준다. 데이터 입력: customers 입력값: https://yeo0616.tistory.com/113 orders 입력값: https://yeo0616.tistory.com/114 use yh_db; select * from orders; select * from customers; -- 100 명 고객 select count(*) from customers; -- 700개 주문 select count(*) from orders; ㄴ 입력한 데이터 값 확인 관계형 데이터베이스 합치기: join -- join 뒤에 갖다 붙일 테이블 명을 쓴다. 하나 더 키워드 on을 쓴다. ㄴ 두개가 같은 컬럼. 연결시키는 .. 더보기
[MySQL] Foreign key 생성 관계형 종류 1. 1대 1 2. 1대 多 3. 多 대 多 - 1대 多 - 어떤 데이터들은 묶음으로 묶어줄 수있다. - 다른 데이터의 정보를 id를 맞추어 묶음을 불러오는 형식으로 진행한다. -> 참고한다고 한다. -> reference -> foreign key 설정 - 테이블 생성 화면 중, 기본 설정 후에, 하단에 Foreign Key 페이지 클릭. - 해당 페이지 진입 후에, 이름을 입력해준다. Foreign Key Name: o_customer_id -> order 테이블의 customer_id이다. 라는 뜻 refenrenced table: 어떤 테이블을 참고할건데? - refenrenced table에 참고할 테이블 선택 후에, 우측, 연결할 컬럼을 정해준다. 해당 컬럼은 데이터 타입이 INT.. 더보기
[MySQL] if() 함수, ifnull() 함수 , 실습 풀이 use yh_db; select * from yh_db.books; use yh_db; select * from books; if() 함수 -- pages가 300보다 크면 long, 그렇지 않으면 short -- if( 조건, 조건이 맞으면~하겠다,조건이 맞지 않으면 ~하겠다. ) ㄴ 첫번째 파라미터: 컬럼이름 및 조건 , ㄴ 두번째 파라미터 : ~하겠다 ㄴ 세번째 파라미터: 그렇지 않으면~하겠다. select *, if( pages >=300, 'long','short') as 'long/short' from books; select * from people; insert into people (first_name) values ('Mike'); ifnull() 함수 -- null이라는 값이 있으면,.. 더보기
[MySQL] ~인것 가져오기: where, and, or, between…and, in(), case when …then.. else… end ~인것 가져오기 : where, and, or, between…and, in(), case when …then.. else… end -- 오늘 가장 중요한 이야기. -- ~인것 가져오기 -- 연도가 2017인 데이터를 가져오시오 select * from books where released_year = 2017; select * from books where released_year = 2010; -- 년도가 2017년이 아닌 데이터만 가져오시오. select * from books where released_year != 2017; -- author_lname이 Harris가 아닌 데이터만 가져오시오. select * from books where author_lname != 'Harris'; sele.. 더보기
[MySQL] 쿼리문으로 테이블 생성 및 현재 시간 기록 설정 쿼리문으로 테이블 생성 및 현재 시간 기록 설정 create table comments( id int unsigned not null Auto_increment primary key, content varchar(100), created_at timestamp default now() ); updated_at: ㄴ default/Expresstion: CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP insert into comments (content) values ('이 사과 정말 맛있습니다.'); insert into comments (content) values ('진짜?'); insert into comments (content) values ('사과 진짜 맛있나.. 더보기
[MySQL] 시간 계산: datediff(), date_add(), interval + 시간 desc people2; insert into people2 (name,birthdate, birthtime, birthdt) values ('Padma','1988-11-11','10:07:35','1988-11-11 10:07:35'), ('Larry','1994-04-22','04:10:42','1994-04-22 04:10:42'); select * from people2; select name, year(birthdate) from people2; 해당 컬럼의 년도를 보여줌 select name, month(birthdate) from people2; 해당 컬럼의 월를 보여줌 select name, day(birthdate) from people2; 해당 컬럼의 날짜를 보여줌 select nam.. 더보기