| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
- react
- fetch
- nextjs
- CSS
- ngrok설치
- nextjs .env
- API token
- npm styled-reset
- Git
- icon
- rewrites
- typescript react
- API 토큰
- github
- SCSS
- dart 변수
- input type=file
- git lab
- bootstrap
- 컨디셔널 렌더링
- styled components
- github io
- next.js css
- react typescript
- createGlobalStyle
- There isn’t anything to compare
- react env
- ngrok실행
- getModifierState
- nextjs 설치
- Today
- Total
꾸준히 성장하는 개발자
[DataBase] 명령어 정리 본문
1.DCL
-사용자 권한 설정 및 회수
-grand(권한부여), revoke(회수)
2.DDL
-데이터 객체 제어
-create, alter, drop
3.DML
-데이터 조작
-insert, update, delete, select
*MariaDB 기본쿼리
1.show : 데이터베이스 & 테이블 목록 확인
2.use : 데이터베이스 선택
* create
1. 객체 생성
- db, table, 사용자, index, ...
2. 기본 문법
- create 객체 객체명 (하위 요소)
3. DB 생성
- create database DB명;
테이블 생성
1. create table 테이블명(
필드명 자료형(길이값) 제약조건,
필드명 자료형(길이값) 제약조건,
필드명 자료형(길이값) 제약조건,
.....
);
2.create table members(
idx int auto_increment primary key,
u_name varchar(20) not null,
u_id varchar(20) not null,
pwd varchar(20) not null
);
*데이터베이스 제약(기본)
1. auto_increment : 자동증가, 자동갱신 X(번호가 지워지더라도 다시 채워지지않음), 기본키로 설정
2. primary key : 기본키, 해당 행을 구분하기 위한 고유값을 가지는 필드에 설정
3. foreign key : 참조키, 다른 테이블과 연관된 데이터가 입력된 필드에 지정
4. not null : 빈 값 허용 안함
5. unique : 중복값 허용 안함
6. check : 정해진 값만 입력 가능
7. default : 기본값 설정
* 테이블 구조 확인
1.describe 또는 desc 테이블명;
* 자료 검색 :select
1. 모든 필드 검색 : select * from 테이블명
2. 일부 필드 검색 : select 필드명, 필드명, ...from 테이블명;
3.
테이블 생성 예시
create table board(
-> idx int primary key auto_increment,
-> title varchar(100) not null,
-> content text not null,
-> author varchar(20) not null,
-> wr_date date not null,
-> view_count int default 0
-> );
테이블 목록 확인
show tables;
일부를 이용하여 다른 테이블 작성
create table board2 as select title, content, author from board;
* 테이블 구조 확인
describe 테이블이름
desc 테이블 이름
테이블 수정
1. 열삭제
alter table 테이블 이름 drop 삭제할 열이름
-alter table board drop veiw_count
2. 열 추가
alter table 테이블이름 add 열이름 자료형
-alter table board add view_count varchar(20)
alter table 테이블이름 add 열이름 자료형 after 필드명
alter table 테이블이름 add 열이름 자료형 first
3. 데이터 타입 변경
alter table 테이블이름 modify 열이름 자료형
-alter table board modify view_count int default 0;
4.필드명 변경
alter table 테이블이름 change 기존열이름 새로운열이름 자료형
-alter table board change cnt2 count int
* 테이블 이름 변경
1. rename table 이전이름 to 새로운이름
-rename table board to notice;
2.alter table 이전이름 rename 새로운이름
-alter table notice to event;
*테이블 데이터 삭제(DML : delete)
1.truncate table 테이블이름
-truncate table event;
*테이블 삭제
1.drop table 테이블 이름
-drop table board2;
* insert : 데이터 입력
1. 전체 필드에 데이터 입력
insert into 테이블이름 values('값1, '값2',...)
-insert into event values(1, '첫번째 글', 'aaaa','관리자','2021-11-24', 0)
2. 일부 필드에 데이터 입력
insert into 테이블이름(필드명1, 필드명2, ...) values ('필드1의 값1', '필드2의 값',...)
-insert into event(title, content,author,wr_date) values ('두번째글', 'bbb', '홍길동','2021-11-24')
insert into event(title, content,author,wr_date) values ('세번째 글', 'ccc', '양세찬','2021-11-22')
insert into event(title, content,author,wr_date) values ('네번째 글', 'ddd', '김종국','2021-11-22')
insert into event(title, content,author,wr_date) values ('다섯번째 글', 'eee', '유재석','2021-11-25')
insert into event(title, content,author,wr_date) values ('여섯번째 글', 'fff', '김종국','2021-11-26')
* select: 데이터 검색
1. 전체 데이터 검색
select * from 테이블이름
-select *from board;
2.일부 데이터 검색
select 필드명, 필드명, ... from 테이블이름
-select idx, title from board;
3. 중복값 제외 검색
select distinct 필드명 from 테이블이름
-select distinct title from board;
4. 별칭 사용
select 필드명 as 별칭, 필드명 as 별칭, ... from 테이블 이름
-select title as '제목' , content as '내용' from board;
5. where절 : 조건검색
select 필드명 from 테이블이름 where 조건
-select * from board where idx=1;
-select title, content from board where idx=1;
-select * from board where author='관리자';
6. in : 비연속 범위 검색
select 필드명 from 테이블이름 where 조건필드 in (검색조건, 검색조건, ...)
-select * from board where idx in (1, 3, 5)
7. between ~ and : 연속 범위 검색
select 필드명 from 테이블 이름 where 조건필드 between 최초범위 and 최종범위
-select * from board where idx between 1 and 5
-select * from board where wr_date between '2021-11-22' and '2021-11-24';
8. order by asc(오름차순)/desc(내림차순) : 정렬
select 필드명 from 테이블명 order by 기준필드 정렬방식
-select * from board order by idx desc;
-select * from board where idx in (1, 3, 5) order by idx desc;
-select * from board where idx in (1, 3, 5) order by idx desc limit 5; ==> 다섯개만 보이게
* update : 데이터 수정
1. update 테이블이름 set 필드명 ='수정할 값'
-update board set view_count=1;
2. update 테이블이름 set 필드명 ='수정할 값', 필드명='수정할 값',...
3. update 테이블이름 set 필드명 ='수정할 값', 필드명='수정할 값',... where 조건
-update board set view_count=2 where idx in(1, 3, 5);
* delete : 데이터 삭제
1. delete from 테이블이름
-delete from board;
2. delete from 테이블이름 where 조건
-delete from board where idx in(1, 3, 5);
* 사용자 생성
create user '아이디'@'호스트(접속위치)' identified by '비밀번호'
-create user 'tester1'@'localhost' identified by '1234'
1.접속위치
-내부 : localhost
-외부 : %
-특정 IP
*권한 확인
show grants for '사용자'@'접속위치';
1. usage : 접속권한
* 사용자 제거
drop user '아이디'@ '접속위치'
-drop user 'tester1'@'localhost'
*grant : 권한 부여
grant 권한, 권한 on DB이름.테이블이름 to '아이디'@'접속위치' with 옵션;
1. tester1 사용자에게 front 라는 DB의 모든 테이블에 select, insert 권한부여
-grant select, insert on front.* to 'tester1'@'localhost';
* revoke : 권한회수
revoke 권한 on DB.table from '아이디'@'접속위치'
-revoke all on *.* from 'tester1'@'localhost';
'그 외...' 카테고리의 다른 글
| [error] pnpm 배포 하는데 에러 발생 (0) | 2025.03.03 |
|---|---|
| [ngrok] ERR_NGROK_3004 / ERR_NGROK_3200 / ngrok-skip-browser-warning (3) | 2025.02.28 |
| [ngrok] ngrok 설치 및 실행 (1) | 2025.02.28 |