[mac]
mariadb 설치
> brew install mariadb
mariadb 설치 후 root 암호 변경
> sudo mysql_secure_installation
mariadb 실행
> brew services start mariadb
> brew services stop mariadb
또는
> mysql.server start
> mysql.server stop
APP끼리 데이터 공유가 힘들다.
다른 APP의 데이터를 덮어쓸 수 있다.
접근 권한 제어가 필요하다.
APP 개발을 할 때마다 파일 관리 APP도 개발해야한다.
mysql -h 서버주소 -u root -p
Enter password: 암호입력
CREATE USER '사용자아이디'@'원격호스트주소' IDENTIFIED BY '암호';
create user 'study'@'localhost' IDENTIFIED BY '1111';
이 경우 study 사용자는 오직 로컬(서버를 실행하는 컴퓨터)에서만 접속 가능한다.
다른 컴퓨터에서 실행하는 MySQL 서버에 접속할 수 없다는 것을 의미한다.
CREATE USER 'study'@'%' IDENTIFIED BY '1111';
이 경우 study 사용자는 원격에서만 접속 가능하다.
mariadb에서는 default 키워드를 사용하지 않는다.
CREATE DATABASE 데이터베이스명 CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE DATABASE studydb CHARACTER SET utf8 COLLATE utf8_general_ci;
GRANT ALL ON 데이터베이스명.* TO '사용자아이디'@'서버주소';
GRANT ALL ON studydb.* TO 'study'@'localhost';
quit or exit -- (프로그램 종료 후)
mysql -u study -p -- (다시 실행)
DROP DATABASE 데이터베이명;
DROP DATABASE studydb;
show databases;
show tables;
query: 조회
Manipulation:
DB 객체(테이블, 뷰, 함수, 트리거 등)를 생성, 변경, 삭제하는 SQL 명령
저장할 데이터구조 정의
create table 테이블명 (
컬럼명 타입 NULL여부 옵션,
컬럼명 타입 NULL여부 옵션,
.
.
.
컬럼명 타입 NULL여부 옵션
);
create table test01 (
name varchar(50) not null,
kor int not null,
eng int not null,
math int not null,
sum int not null,
aver float not null
);
use 사용할 데이터베이스명;
use studydb;
insert into test1 (no, name) values(1, '홍길동');
select no, name from test1;
나열된 순서대로 데이터 추출
show tables; -- 전체 테이블 정보
describe 테이블명;
desc 테이블명;
drop table 테이블명;
데이터를 입력하지 않아도 된다.
create table test1 (
no int,
name varchar(20)
);
데이터를 입력하지 않으면 입력/변경 거절!
create table test1(
no int not null,
name varchar(20)
);
입력 값을 생략하면 해당 컬럼에 지정된 기본값이 대신 입력된다.
create table test1(
no int not null,
name varchar(20) default 'noname',
age int default 20
);
컬럼에 default 옵션이 있는 경우,
create table test1(
c1 int,
c2 float,
c3 numeric(6,2), /* 소수점 자릿수를 지정하면 부동소수점으로 사용 */
c4 numeric -- decimal 과 같다
);
DBMS 중에는 고정 크기인 컬럼의 값을 비교할 때 빈자리까지 검사하는 경우도 있다.
즉 c1='abc'에서는 데이터를 찾지 못하고, c1='abc '여야만 데이터를 찾는 경우가 있다.
그러나 mysql은 고정크기 컬럼이더라도 빈자리를 무시하고 데이터를 찾는다.
create table test1(
name varchar(20) primary key,
kor int,
eng int,
math int
);
create table test1(
name varchar(20) primary key,
age int primary key,
kor int,
eng int,
math int
); /* 실행 오류 */
constraint 제약조건이름 primary key (컬럼명, 컬럼명, ...)
create table test1(
name varchar(20),
age int,
kor int,
eng int,
math int,
constraint test1_pk primary key(name, age)
);
create table test1(
no int primary key, /* 학번 */
name varchar(20),
age int,
kor int,
eng int,
math int
);
insert into test1(no,name,age,kor,eng,math) values(1,'a',10,90,90,90);
insert into test1(no,name,age,kor,eng,math) values(2,'a',11,91,91,91);
insert into test1(no,name,age,kor,eng,math) values(3,'b',11,81,81,81);
insert into test1(no,name,age,kor,eng,math) values(4,'c',20,81,81,81);
insert into test1(no,name,age,kor,eng,math) values(5,'c',20,81,81,81);
create table test1(
no int primary key,
name varchar(20),
age int,
kor int,
eng int,
math int,
constraint test1_uk unique (name, age)
);
create table test1(
no int,
name varchar(20),
age int,
kor int,
eng int,
math int,
constraint primary key(no),
constraint test1_uk unique (name, age)
);
create table test1(
no int,
name varchar(20),
age int,
kor int,
eng int,
math int
);
alter table test1
add constraint test1_pk primary key(no),
add constraint test1_uk unique (name, age);