Sql
-
Foreign key ( 외래키 )Sql 2023. 3. 17. 11:41
외래키 ( Foreign key ) : 여러개의 테이블이 있을 때 데이터 무결성을 유지하기 위해 중요하다. 외래키를 설정하면 참조 무결성을 강제할 수 있으며, 다른 테이블의 데이터를 변경할 때 해당 외래키를 참조하는 모든 테이블의 데이터도 함께 변경된다. 이를 통해 데이터 일관성과 정확성을 유지할 수 있다. - 외래키로 설정하기 위해서는 기본적으로 primary key 설정이 되어있어야한다. - 외래키와 기본키의 타입이 일치해야한다. - 다음과 같이 예를 들 수 있다. 위 예시에서는 orders 테이블의 product_id 피륻가 products 테이블의 product_id 필드를 참조하도록 외래키 제약조건을 설정했다. orders 테이블의 product_id 필드는 products 테이블의 produc..
-
SQL 첫걸음 CH07. 복수의 테이블 다루기Sql 2023. 3. 17. 09:06
1 _ 집합 연산 : 데이터 베이스의 데이터를 집합으로 간주해 다루기 쉽게 하자. 또한 집합으로 취급하기 때문에 일반 연산과는 다름을 주의하자 - UNION : 두 집합을 합치는 것으로 '합집합'의 개념과 같다. 따라서 , 중복되는 값이 있다면 하나로 취급한다. - {1,2,3} 인 71_a와 {2, 10, 11}인 71_b를 UNION 하자 중복값인 2는 1개로 취급하는 열이 생성됬다. - 위와같이 열의 갯수가 다른 테이블을 UNION 하고싶다면, 열의 갯수를 맞춰줘야한다. - sample 71_a,b와 맞춰서 열의 갯수 1개가 되도록 sample31 에서 특정 열 1개를 선택해서 union 했다. ** union 구에 order by를 사용하려면 : union 구의 제일 마지막 select 구에 or..
-
SQL 첫걸음 CH05. 집계와 서브쿼리Sql 2023. 3. 15. 11:18
집계함수 COUNT(집합) SUM(집합) AVG(집합) MIN(집합) MAX(집합) 1 _ 행 개수 구하기 COUNT - count 예제 - sample51의 행의 갯수를 알 수 있다. - 명령어가 from -> where -> select 순서로 진행되기 때문에 name이 A인 행 2개를 세었다. ** count() 내부에 *(전체) 로 집합을 설정하면, 하나의 행에 NULL이 아닌 열이 하나라도 있으면 모두 센다 - DISTINCT로 중복 제거 - 해당 열을 중복값 없이 출력할 수 있다. - count 내부에 distinct를 넣는 것으로 null 값을 세지 않을 수도 있다. 2 _ COUNT 이외의 집계함수 : 기본적으로 집계함수에서는 NULL 값을 무시한다 ( 완전히 존재하지 않는 것으로 취급한다..
-
SQL 첫걸음 CH04. 데이터의 추가, 삭제, 갱신Sql 2023. 3. 15. 10:23
1 _ 행 추가하기 ( INSERT ) : insert할 때는 열마다 지정되어있는 자료형, 열의 갯수, 제약을 고려해야한다. INSERT 명령 INSERT INTO 테이블명 VALUES(값1, 값2, ...) 값을 넣을 열을 지정해서 넣을 수 있다. INSERT INTO 테이블명(열명1, 열명2, ...) VALUES(값1, 값2, ...) - INSERT 방법 3가지 예제 - 디폴트 값을 넣어보자 : sample411 의 desc를 보고 같은 형태의 테이블을 생성하려면 - d열을 생성할 때 default 값을 0으로 준다 - default 값을 선언하면, 아무 값도 주지 않았을 때 NULL 이 아닌 default 값이 들어간다. 2 _ 삭제하기 DELETE DELETE FROM 테이블명 WHERE 조건..
-
SQL 첫걸음 CH03. 정렬과 연산Sql 2023. 3. 14. 10:43
1 _ 정렬 ORDER BY 어떤 조건으로 열들을 정렬 할지 정할 수 있다. SELECT 열명 FROM 테이블명 WHRER 조건식 ORDER BY 열명 (DESC or ASC) DESC : 내림차순 ASC : 오름차순 - 내림인지 오름인지 정하지 않았다면 기본적으로 오름차순으로 지정된다 ( 작은 수가 위에 ) 숫자형 자료의 경우 오름, 내림차순은 숫자를 기준으로 한다. 년도 : 1000년 문자열 자료 칸에 숫자가 입력된 경우 이는 숫자가 아니라 문자이므로 오름차순 했을 때 다음과 같은 결과가 나온다 - a열의 경우 문자열이기 때문에 숫자를 넣어도 사전식 순서에 따라 10 , 11 보다 2가 더 크다. 2 _ 복..
-
SQL 첫걸음 ch02. 테이블에서 데이터 검색Sql 2023. 3. 13. 09:34
1 _ 테이블 불러오기 Select * from 테이블명; * : 모든 열을 뜻한다. select 열명 from 테이블명 -> 열 하나만 가져올 수도 있다. - sql은 대소문자 구분을 하지 않는다. 다른 데이터베이스들은 대소문자 구분을 하는 경우가 많으니 주의하자. 2 _ 자료형 - INTERGER 형 : 정수값을 저장할 수 있는 자료형 - CHAR 형 : 고정길이 문자열, 최대길이보다 문자열의 길이가 작아도 남은 부분을 공백문자로 채운 후 저장함. - VARCHAR 형 : 가변 길이 문자열, 문자열의 길이에 따라 저장 공간이 변하고 최대치를 넘길 수 없다. - DATE 형 : 날짜를 저장할 수 있다. - TIME 형 : 시간을 저장할 수 있다. 3 _ 검색조건 지정하기 select 열1, 열2 for..
-
SQL 첫걸음 CH06. 데이터베이스 객체 작성과 삭제Sql 2023. 3. 9. 15:59
처음 RDBMS 소프트웨어를 설치하면 데이터베이스는 비어있는 상태다. 여기에 테이블이나 뷰, 인덱스, 프로시저 등의 데이터베이스 객체를 작성해 데이터베이스를 구축한다. 1 _ 데이터베이스 객체 : 데이터베이스의 객체란 다른 프로그램에서 사용하는 객체와는 다른 의미다. 데이터베이스의 객체는 테이블, 뷰, 인덱스 등 데이터베이스 내에 정의하는 모든 요소를 말한다. 따라서 객체는 데이터베이스 내에 실체를 가지고있다. 객체를 조작하는 SQL의 명령(SELECT , INSERT)은 외부에서 온 것이기 때문에 실체가 없다 - 데이터베이스 내부 명명규칙 기존 이름이나 예약어와 중복하지 않는다. 숫자로 시작할 수 없다. 언더스코어(_) 이외의 기호는 사용할 수 없다. 한글을 사용할 때는 더블쿼트(MySQL에서는 백쿼트..
-
SQL 첫걸음 CH01 데이터베이스 소개Sql 2023. 3. 9. 10:08
1 _ 데이터베이스 : 데이터의 집합이며 특정 데이터를 확인하고 싶을 때 간단하게 찾아낼 수 있도록 정리된 형태 DB : 저장장치 내에 정리되어 저장된 데이터의 집합 DBMS : 데이터베이스 관리 시스템 DBMS가 필요한 이유 - 생산성 : 데이터 검색, 추가, 삭제, 갱신 같은 처리를 DBMS 기본으로 제공하기 때문에 기본 기능을 구현하는데 시간과 노력을 들일 필요가 없다. - 기능성 : 데이터 베이스를 다루는 기능을 많이 제공한다. 복수의 요청에 대응하거나 대용량의 데이터를 저장하고 고속으로 검색하는 기능 등을 제공한다. - 신뢰성 : 많은 요청에 대응할 수 있도록 만들어져있다. DBMS는 컴퓨터 여러대를 두고 소프트웨어를 통해 확장성(Scalability)과 부하분산(Load balancing)을 ..