연두색연필
LimePencil's Log
연두색연필
전체 방문자
오늘
어제

About Me

  • GitHub
  • Instagram
  • Gmail

인기 글

  • 분류 전체보기 (70)
    • Machine Learning (3)
      • MNIST (2)
    • PS & Algorithm (9)
    • Web (4)
      • HTML (1)
      • JavaScript (3)
    • Rust (2)
      • The Rust Programming Langua.. (2)
    • 논문 리뷰 (12)
      • Reinforcement Learning (10)
      • Computer Vision (2)
    • DevOps (17)
      • Docker (9)
      • Kubernetes (8)
    • Development (6)
      • SQL (6)
    • 잡다한 것들 (15)
      • 부스트캠프 AI Tech 4기 (13)

최근 댓글

Tag

  • Python
  • K8s
  • ML
  • Kubernetes
  • 도커
  • 백준
  • docker
  • 쿠버네티스
  • 부스트캠프
  • 파이썬
06-22 15:56
hELLO · Designed By 정상우.
연두색연필

LimePencil's Log

Development/SQL

[SQL 배우기] Section 8. Creating Databases and Tables

2024. 3. 12. 01:03

 

 

Data types in SQL:

  • Boolean
    • True, False
  • Character
    • char, varchar, text
  • Numeric
    • integer, float
  • Temporal
    • date, time, timestamp, interval
  • UUID
  • Array
  • JSON

 

PostgreSQL: Documentation: 16: Chapter 8. Data Types

 

Primary key: a column or a group of columns used to identify a row uniquely in a table\

  • represented by [PK] in pgAdmin

 

Foreign key: a field or group of fields that references the primary key of the other table

 

Constraint: rules enforced on data columns table

  • prevent invalid data
  • Divided into Column constraint and Table constraint:
  • Common Column constraint:
    • NOT NULL constraint
    • UNIQUE constraint
    • PRIMARY Key
    • FOREIGN Key
    • CHECK constraint
      • ensures that all values in a column satisfy certain conditions
    • EXCLUSION constraint
      • ensures that if any two rows are compared on the specified column or expression using the specified operator, not all of these comparisons will return TRUE
  • Common Table constraint:
    • **CHECK (**condition)
      • to check conditions when inserting or updating data
    • REFERENCES
      • to constrain the value stored in the column that must exist in a column in another table
    • UNIQUE (column_list)
      • forces the values stored in the columns listed inside the parentheses to be unique
    • PRIMARY KEY (column list)
      • allows you to define the primary key that consists of multiple columns

 

The general syntax for creating a table:

  • CREATE table table_name ( column_name TYPE column_constraint, column_name TYPE column_constraint, table_constraint table_constraint ) INHERITS existing_table_name;

 

SERIAL: creates a sequence object and sets the next value generated by the sequence as the default value for the column

  • perfect for PRIMARY key
  • does not adjust for deletion

 

INSERT: allow to add rows to a table

  • general syntax: INSERT INTO table (column1, column 2, …) VALUES (value1, value2, …), (value1, value2, …), ….;
  • SERIAL columns do not need to be provided a value

 

UPDATE: allows change of values of the columns

  • general syntax: UPDATE table SET column1 = value1, column2 = value2, … WHERE condition;
  • without WHERE, everything is reseted
  • update from another table: UPDATE tableA SET original_col = tableB.new_col FROM tableB WHERE tableA.id = tableB.id
  • return affected rows UPDATE table SET column=value RETURNING column1, column2

 

DELETE: remove rows from the table

  • DELETE FROM table WHERE row_id = 1
  • DELETE FROM tableA USING tableB WHERE tableA.id = tableB.id

 

ALTER: allows for changes to an existing table structure

  • adding, dropping, or renaming columns
  • changing a column’s data type
  • set DEFAULT values for a column
  • Add CHECK constraints
  • Rename table
  • General Syntax ALTER TABLE table_name action

 

DROP: allows the complete removal of a column in a table

  • General Syntax ALTER TABLE table_name DROP COLUMN col_name

 

CHECK: allows the creation of more customized constraints that adhere to a certain condition

'Development > SQL' 카테고리의 다른 글

[SQL 배우기] Section 6. Advanced SQL Commands  (0) 2024.03.12
[SQL 배우기] Section 5. JOINS  (0) 2024.03.12
[SQL 배우기] Section 3. GROUP BY Statements  (0) 2024.03.12
[SQL 배우기] Section 2. SQL Statement Fundamentals  (0) 2024.03.12
[SQL 배우기] Section 1. Course Introduction  (0) 2024.03.12
    'Development/SQL' 카테고리의 다른 글
    • [SQL 배우기] Section 6. Advanced SQL Commands
    • [SQL 배우기] Section 5. JOINS
    • [SQL 배우기] Section 3. GROUP BY Statements
    • [SQL 배우기] Section 2. SQL Statement Fundamentals
    연두색연필
    연두색연필
    ML, Programming, PS, 삶의 순간을 기록

    티스토리툴바