mysql > bin >
cmd에서 mysql bin폴더로 이동
mysql 입력
mysql -uroot -p
mysql 접속완료
show databases;
새로운 database 생성
create database node_db default character set utf8;
조회
show databases;
방금 생성한 db 사용하기
use node_db;
테이블 만들기( -> : 이어진다는 뜻)
create table todo(
-> id INT(11) NOT NULL AUTO_INCREMENT,
-> title VARCHAR(200) NOT NULL,
-> curdate TEXT NULL,
-> primary key(id));
만들어진 테이블 확인하기
show tables;
todo table의 필드 확인하기
desc todo;
데이터 넣기 (id는 PK이므로 넣을필요 없음)
insert into todo (title, curdate) values ('오늘은 발표', '2022.11.14');
데이터 조회 (todo 테이블의 전체값 조)
select * from todo;
데이터 다시 입력
insert into todo (title, curdate) values ('점심먹기', '2022.11.14');
insert into todo (title, curdate) values ('점심먹고 발표하기', '2022.11.14');
데이터 다시 조회
select * from todo;
지정된 열만 가져오고 싶을 때 (title만 가져오기)
select title from todo;
지정된 열만 가져오고 싶을 때 (id 및 title 가져오기)
select id, title from todo;
오름차순 정렬 (title 오름차순)
select * from todo order by title;
내차순 정렬 (title 내림차순)
select * from todo order by title desc;
where문 (title이 2보다 큰 모든값 출)
select * from todo where id > 2;
기존 테이블에 컬럼추가
ALTER TABLE todo ADD COLUMN writer varchar(30);
기존 테이블 데이터 수정
id 1번의 writer변경
update todo set writer = 'can' where id = 1;
입력된 결과 select
id 2번의 날짜 변경
update todo set curdate = '2022.11.15' where id = 2;
수정된 결과 select
id 3번 삭제
delete from todo where id = 3;
삭제된 결과
insert into todo(title, curdate, write) values('특강 수강하기', '2022.11.15','kkk');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'write) values('특강 수강하기', '2022.11.15','kkk')' at line 1
write > writer 오타주의
insert into todo(title, curdate, writer) values('특강 수강하기', '2022.11.15','kkk');
4번에 입렵됨....
테이블 이름 변경
rename table todo to todo_bk;
타입 변경
alter 테이블이름 login modify 컬럼 varchar(10000);
SQL left join link
https://piaocanyi.tistory.com/320
mysql left join
todo table에 write_id가 없어서 추가 ex) update todo set write_id = 1 where id = 1; writer table과 todo table left join (writer 테이블의 id가 todo 테이블의 write_id와 같을 경우) select todo.id as todo_id, title, curdate, name, profile
piaocanyi.tistory.com