지난 시간 문제풀이
select *
from Products
order by Price asc;
select *
from Products
order by Price asc, ProductID desc;
select *
from OrderDetails
order by OrderID asc, Quantity desc;
select *, ProductID + SupplierID + CategoryID as NewId
from Products;
select Price
from Products
where Price between 20 and 50;
select *
from Products
where Price < 20 or Price > 50;
select *
from Customers
where Country in ('Brazil', 'Spain', 'France');
select *
from Customers
where CustomerName like '%Co';
select *
from Customers
where Country like 'A%';
select *
from Products
where ProductName like 'C%e';
select *
from Employees
where FirstName not like 'A%';
select ProductID, ProductName, max(Price)
from Products;
select round(avg(Price),1)
from Products;
select datediff(now(), OrderDate)
from Orders;
<CHAT GPT를 이용한 예제용 테이블 만들기>
-- 회원 테이블
CREATE TABLE Members (
MemberID INT PRIMARY KEY AUTO_INCREMENT,
Username VARCHAR(50) NOT NULL UNIQUE,
Password VARCHAR(50) NOT NULL,
RealName VARCHAR(50) NOT NULL,
Nickname VARCHAR(50) NOT NULL UNIQUE
);
-- 게시판 테이블
CREATE TABLE Boards (
BoardID INT PRIMARY KEY AUTO_INCREMENT,
BoardName VARCHAR(50) NOT NULL UNIQUE
);
-- 게시물 테이블
CREATE TABLE Posts (
PostID INT PRIMARY KEY AUTO_INCREMENT,
Title VARCHAR(100) NOT NULL,
Content TEXT NOT NULL,
AuthorID INT NOT NULL,
Views INT DEFAULT 0,
ModifiedDate DATETIME,
CreatedDate DATETIME NOT NULL,
BoardID INT NOT NULL,
FOREIGN KEY (AuthorID) REFERENCES Members (MemberID),
FOREIGN KEY (BoardID) REFERENCES Boards (BoardID)
);
-- 좋아요 테이블
CREATE TABLE Likes (
LikeID INT PRIMARY KEY AUTO_INCREMENT,
MemberID INT NOT NULL,
PostID INT NOT NULL,
UNIQUE (MemberID, PostID),
FOREIGN KEY (MemberID) REFERENCES Members (MemberID),
FOREIGN KEY (PostID) REFERENCES Posts (PostID)
);
-- 댓글 테이블
CREATE TABLE Comments (
CommentID INT PRIMARY KEY AUTO_INCREMENT,
Content TEXT NOT NULL,
AuthorID INT NOT NULL,
PostID INT NOT NULL,
FOREIGN KEY (AuthorID) REFERENCES Members (MemberID),
FOREIGN KEY (PostID) REFERENCES Posts (PostID)
);