본문 바로가기
Service/SQL

SQL - 1

by 포항돼지 2021. 5. 14.

SQL 첫걸음

https://www.w3schools.com/sql/trysql.asp?filename=trysql_select_all

SQL 실습 웹사이트

Select *(전부 선택) FROM(테이블) Customers

SELECT DISTINCT(뚜렷한, 분명한)(중복되는 데이터들은 제거하고 보여줘 라는뜻) City,Country FROM Customer.

데이터를 보여줄때, 오름차순 내림차순으로 보여주기도 가능하다.

DESCENDING(오름차순)

ASC(내림차순)

SELECT * FROM Customers ORDER BY CustomerID DESC; (Customers 테이블의 CustomerID 높은 순서(4321)부터 보여줘 라는 ) 오름차순

SELECT * FROM Customers ORDER BY CustomerName ASC; (CustomerName 알파벳 abcd(1234) 내림차순으로 보여줘)

 

테이블에 별칭을 넣을 수도 있음.

Select customerid as 회원번호, customername회원이름 (customername 회원이름으로 변경, as 구문 대신 스페이스 넣어도 사용 가능)

from customers

order by customername asc;

 

select customerid % 2 from customers 퍼센트 연산자는, 나머지를 보여주는거. 홀짝 나누기용으로 많이 쓰임

select Address || City || Country from customers

파이프를 쓰면 합치는 기능임 공백을 넣고 싶으면, 문자열로 넣어줘야함

 

 

True(1) and False(0) = False(0) 둘중에 하나가 나오면 무조건 0인거 (논리 곱이라고함)

True or False = True 1 더하기 0 1 (논리 ) 더했을때 True 값이 나오면(1)무조건 True 둘다 0일때만 무조건 0

not Ture = False (not 반대)

 

select * from customers where customerid between 30 and 50;

내가 앞에서 선택한 customers table안의 커스터머 id 로우안에 30 에서 50 사이의 값들을 출력해라 (AND 30 50 포함한다)

select * from customers where customerid IN(10,20,30) 로우안의 10,20,30 값만 출력해라

select * from customers where customerid LIKE '1_'; (1_ 포함된 것을 출력해라)

 

paullab, paultest, paulcode

like '&paul%' -> 앞뒤로 paul 포함하는 모든 문자열을 프린트해라, 원래 ‘paul___’ 이런식으로 언더바 갯수 와일드카드 숫자에 맞게 박아야됨

is null -> 테이블 프린트 하는 명령어

비트단위 연산자 &(and) | (or)

5 & 7 했을때 5 나오는 이유

5 = 101 , 7 = 111 and 연산하면 101 5 나옴

비교 연산자.

select * from customers where customerid > 50 and city='London';

모든 customers 테이블안에 customerid 50보다 큰것 ‘London’ 스트링 가지고 있는 것만 출력해라

select * from customers where customerid > 50 or city='London';\

모든 customers 테이블안에 customerid 50보다 큰것 ‘London’ 스트링 가지고 있으면 무조건 출력해라, 50보다 데이터는 출력됨. 그리고 London 가지고 있는 50보다 작은 값도 출력됨

 select * from customers where customerid > 50 or city!='London';

!= 써서 City 런던이 아닌것도 확인 가능.

select * from customers where customerid > 50 or not (city='London'); not 이랑 등호써서 같은 출력 가능

insert into customers (customername, city, country)

values ('Hangil Ko', 'Pohang Si', 'Korea');

Insert 써서 데이터 넣기 가능

92 Hangil Ko null null Pohang Si null Korea

넣은거는 null 나와있음.

select * from customers where Address is null;

명령어 써서 다시 출력 가능

update customers

set customername='하르방',city = '병신', country='착짱죽짱'

where customerid = 1;

 

update명령어는 기존에 존재하고 있는 데이터를 업데이트 하는거.

Where customerid 1번인곳에 업데이트를 저래 한다는 말임

update customers

set customername='하르방',city = '병신', country='착짱죽짱'

이렇게 where customerid = 1; 안해주면 1번뿐만아니라 모든 테이블의 데이터 값이 바뀜

데이터 지울때도 마찬가지. Where 안쓰면 지워짐.

delete from customers where city='Pohang Si';

Select top 3 * from customers; 하면 가장 위의 3개의 값을 보여줌

Select * from customers limit 10;

Create DB 테이블을 만듬.

DROP DB 테이블을 지울수 있게해줌.

많이쓰는 명령어 : show databases; 전체 데이타 베이스를 보여줌

Show tables; 전체 데이타 베이스 테이블 보여줌

Desc table_name; describe, 테이블 네임에서 각각의 어떤 컬럼 값을 가지고 있는지 보여줌.

 

중요한 SQL 함수들.

1.       Small case capital 바꾸는 함수

select CustomerID, CustomerName,

                LOWER(CustomerName) AS 소문자,

    UPPER(CustomerName) AS 대문자

from customers

 

select upper('hello world') ->

upper('hello world')
HELLO WORLD

 

Select lower(‘Hello World’)

lower('Hello World')
hello world

 

2.       문자열 잘라네는 substr 기능

select substr('hello world', 1, 3) 하면 hel 까지 printed

python splicing 이랑 같은 기능

select substr('hello world', 7, 3) 하면 7번째부터 3 출력 ->wor

python이랑 똑같이 마이너스 인덱싱도 가능 (거꾸로 카운트)

select substr('hello world', 7) 이렇게 뒤에 아무것도 안쓰면 뒤에 남은 것들 모두다 출력 해줌.

substr('hello world', 7)
world

 

select length('hello world') : 문자열의 갯수 출력

length('hello world')
11

Select count(‘hello world’) : 가지고 있는 값의 갯수를 출력 (pointer 갯수 확인하는거인듯)

count('hello world')
1

 

select replace('hello world','world','sql') : 스트링 replace해주는기능

replace('hello world','world','sql')
hello sql

 

select concat('hello','sql') : 문자열 연결 해주는 기능