반응형

 

 

정보는 표에 저장이 된다.

 

Schema(DB) : 서로 연관된 데이터들을 그룹핑 해준다.

 

Database Server : Scheme들이 저장되어 있는 것

 

즉, MySQL을 설치했다는 건 Database Server를 설치한 것이다.

 

데이터베이스 서버 → 스키마(DB) → tables

 


데이터베이스(스키마) 생성

 

create database [DB 이름];

 

데이터베이스(스키마) 삭제

 

drop database [DB 이름];

 

데이터베이스(스키마) 사용 지정

use [DB 이름];

 


Table 구조

row, record, 행 : table에서 행을 나타내는 용어

 

column : table에서 열을 나타내는 용어

 

 

Table ex

create table topic(
	id INT(11) NOT NULL AUTO_INCREMENT,
	title VARCHAR(100) NOT NULL,
	description TEXT NULL,
	created datetime not null,
	author VARCHAR(30) null,
	profile varchar(100) null,
	primary key(id));
);

 


Database 혹은 Table 확인

show databases;

show tables;

 

table 구조 확인

desc [table 이름];

 

Table 이름 변경

rename table topic to topic_backup;

 

Table의 구조를 바꿀 때

// topic 테이블을 바꾸는데 컬럼을 추가한다.
alter table topic add column author_id INT(11);

 


data 삽입(Create)

insert into topic (title,description,created,author,profile) VALUES("MySQL","MySQL is....",now(), "sean", "R&D");

 

data 가져오기(READ)

// topic 테이블의 모든 데이터를 가져오기
select * from topic;

// topic 테이블에서 id, title, created, author 컬럼만 추출하는데, author가 'sean'인 컬럼만 id를 내림차순으로 2개만 출력한다.
select id, title, created, author from topic where author="sean" order by id desc limit 2;

 

data 업데이트(Update)

syntax) update [table 이름] set [수정할 컬럼이름]='[수정할 값]', [수정할 컬럼이름]='[수정할 값]' where [변경할 데이터의 id];
update topic set description='Oracle is...', title='Oracle' where id=2;

 

data 삭제(Delete)

delete from topic where id=5;

 

data 전체 삭제(truncate)

truncate [테이블명]

 


관계형 DB(Join)

 

topic 테이블 생성

CREATE TABLE topic (
  id int(11) NOT NULL AUTO_INCREMENT,
	title varchar(30) NOT NULL,
  description text,
  created datetime NOT NULL,
  author_id int(11) DEFAULT NULL,
  PRIMARY KEY (id)
);

 

topic 테이블에 데이터 생성

INSERT INTO topic VALUES (1,'MySQL','MySQL is...','2018-01-01 12:10:11',1);
INSERT INTO topic VALUES (2,'Oracle','Oracle is ...','2018-01-03 13:01:10',1);
INSERT INTO topic VALUES (3,'SQL Server','SQL Server is ...','2018-01-20 11:01:10',2);
INSERT INTO topic VALUES (4,'PostgreSQL','PostgreSQL is ...','2018-01-23 01:03:03',3);
INSERT INTO topic VALUES (5,'MongoDB','MongoDB is ...','2018-01-30 12:31:03',1);

 

author 테이블 생성

CREATE TABLE author (
  id int(11) NOT NULL AUTO_INCREMENT,
  name varchar(20) NOT NULL,
  profile varchar(200) DEFAULT NULL,
  PRIMARY KEY (id)
);

 

author 테이블에 데이터 생성

INSERT INTO author VALUES (1,'sean','developer');
INSERT INTO author VALUES (2,'duru','database administrator');
INSERT INTO author VALUES (3,'taeho','data scientist, developer');

 

join ex 1) MySQL에게 topic 테이블에 있는 모든 데이터를 출력하는데, author_id와 같은 값을 가지고 있는 author 테이블에서의 id 컬럼의 데이터를 topic 테이블에 이어 붙혀라고 명령할 때

select * from topic left join author ON topic.author_id = author.id // ON은 기준이 되는 것을 알려주는 키워드이다.

 

join ex 2) MySQL에게 topic 테이블에 있는 모든 데이터를 출력하는데, author_id와 같은 값을 가지고 있는 author 테이블에서의 id 컬럼의 데이터를 topic 테이블에 이어 붙혀라고 명령하면서 author_id와 id의 값은 안 보고 싶을 때

select topic.id, title, descriptioin, created, name, profile from topic left join author ON topic.author_id = author.id // ON은 기준이 되는 것을 알려주는 키워드이다.

 


 

반응형

+ Recent posts