Skip to main content
mysql

3주차

By 9월 18th, 2024No Comments

데이터베이스 선택하기

 

데이터베이스 전체 목록보기

show databases;

 

데이터 베이스 사용

-- use db명; --
use employees;

 

선택한 데이터베이스의 전체 테이블 상태보기, 테이블 종류보기, 필드구성요소보기

show table status;
show tables;
describe 테이블명;

-- 테이블명이 usertbl이라면 --
describe usertbl;
desc usertbl;

 

선택한 테이블 내용 보기

사용중인 데이터베이스의 테이블명을 불러오거나 db명.테이블명 연결해서 적어도 된다.
보통 현재 사용중인 DB를 선택한 경우 테이블 이름만 적어서 사용한다.
데이터베이스를 다른것이 선택되어 있을때 다른db명을 바로 조회할때 db명도 같이 적어준다.

select * from employees;
select * from employees.employees;

 

원하는 필드(컬럼)만 불러오고 싶을때 (employees의 titles 테이블을 조회기준)

select emp_no,title, from_date, to_date from titles;

select * from titles;

 

불러오는 숫자 제한설정하기

limit 옵션

 


신규 데이터베이스 생성하기

 

DROP DATABASE IF EXISTS sqldb;   -- 기존에 sqldb 이름이 있다면 삭제시켜라 , 매우 조심해야함 --

CREATE DATABASE sqldb; 

USE sqldb;

CREATE TABLE usertbl (
    userID char(8) not null PRIMARY KEY,  -- 회원 아이디 --
    name VARCHAR(10) NOT NULL,  -- 회원이름 --
    birtthYear int not null, -- 생년월일 --
    addr char(2) not null, -- 지역구분 --
    mobile1 char(3), -- 휴대폰 앞자리 --
    mobile2 char(8), -- 휴대폰 중간,뒷 --
    height SMALLINT, -- 키 --
    mDate DATE -- 회원가입일 --
);

CREATE TABLE buytbl 
(
    num int AUTO_INCREMENT not null PRIMARY	KEY,
    userID char(8) not null,
    prodName char(6) not null,
    groupName char(4), 
    price INT not null,
    amount SMALLINT not null,
    FOREIGN KEY(userId) REFERENCES usertbl(userID)
);
show TABLE STATUS;
show TABLES;
desc usertbl;

 

샘플 데이터 입력하기

 

INSERT INTO usertbl VALUES('LSG', '이승기', 1987, '서울', '011', '1111111', 182, '2008-8-8');
INSERT INTO usertbl VALUES('KBS', '김범수', 1979, '경남', '011', '2222222', 173, '2012-4-4');
INSERT INTO usertbl VALUES('KKH', '김경호', 1971, '전남', '019', '3333333', 177, '2007-7-7');
INSERT INTO usertbl VALUES('JYP', '조용필', 1950, '경기', '011', '4444444', 166, '2009-4-4');
INSERT INTO usertbl VALUES('SSK', '성시경', 1979, '서울', NULL  , NULL      , 186, '2013-12-12');
INSERT INTO usertbl VALUES('LJB', '임재범', 1963, '서울', '016', '6666666', 182, '2009-9-9');
INSERT INTO usertbl VALUES('YJS', '윤종신', 1969, '경남', NULL  , NULL      , 170, '2005-5-5');
INSERT INTO usertbl VALUES('EJW', '은지원', 1972, '경북', '011', '8888888', 174, '2014-3-3');
INSERT INTO usertbl VALUES('JKW', '조관우', 1965, '경기', '018', '9999999', 172, '2010-10-10');
INSERT INTO usertbl VALUES('BBK', '바비킴', 1973, '서울', '010', '0000000', 176, '2013-5-5');
INSERT INTO buytbl VALUES(NULL, 'KBS', '운동화', NULL   , 30,   2);
INSERT INTO buytbl VALUES(NULL, 'KBS', '노트북', '전자', 1000, 1);
INSERT INTO buytbl VALUES(NULL, 'JYP', '모니터', '전자', 200,  1);
INSERT INTO buytbl VALUES(NULL, 'BBK', '모니터', '전자', 200,  5);
INSERT INTO buytbl VALUES(NULL, 'KBS', '청바지', '의류', 50,   3);
INSERT INTO buytbl VALUES(NULL, 'BBK', '메모리', '전자', 80,  10);
INSERT INTO buytbl VALUES(NULL, 'SSK', '책'    , '서적', 15,   5);
INSERT INTO buytbl VALUES(NULL, 'EJW', '책'    , '서적', 15,   2);
INSERT INTO buytbl VALUES(NULL, 'EJW', '청바지', '의류', 50,   1);
INSERT INTO buytbl VALUES(NULL, 'BBK', '운동화', NULL   , 30,   2);
INSERT INTO buytbl VALUES(NULL, 'EJW', '책'    , '서적', 15,   1);
INSERT INTO buytbl VALUES(NULL, 'BBK', '운동화', NULL   , 30,   2);

 

insert 구문에서 모든값을 입력하지 않고 일부만 입력하는 구문 알아보기

insert into usertbl (name, birthYear, addr) values (‘김성길’,1979,’경북’);   — 오류가 나는 원인을 생각해보기 —

부분 입력을 위해서  프라이머리 키 값은 반드시 입력해야 한다.

 

insert into usertbl (userid, name, birthYear, addr) values ('KSS','김성실',1979,'경북');   -- 부분 입력 -- 

update usertbl set mobile1 ='010', mobile2='85858585', height='170', mDate='2024-09-09' where userid ='KSS';  -- 빠진 부분을 업데이트 처리하기 --

insert 3건을 한구문에 입력하기

 

insert into  테이블이름 values (값1, 값2…) , (값3, 값4…), (값5,값6…)

 

 

where 조건식 살펴보기

where는 데이터를 불러올때 조건을 걸 수 있다.

select name from usertbl where ='이승기';  --> 틀렸음 어디가...

select name, height from usertbl;

select name, height from usertbl where birthYear >= 1970 or height >= 182;

select name, height from usertbl where height >= 180 and height <= 183;

select name, height from usertbl where height between 180 and 183;

select name, height from usertbl where height >= 180 and height <= 183;

select name, height from usertbl where addr='경북' or addr='서울' or addr='전북';

select name, height from usertbl where addr in ('경북','서울','전북');

select name, height from usertbl where name like '김%';
select name, height from usertbl where name like '%김%';
select name, height from usertbl where name like '%김';
select name, height from usertbl where name like '_종신';
select name, height from usertbl where name like '김__';

employee 테이블에서 아래 쿼리구문 확인해보기

select emp_no, birth_date from employees where emp_no < 15000 and birth_date like '%1953%' order by emp_no desc limit 2;

 

Leave a Reply