본문 바로가기

SQL

SQL Syntax

sudo mysql -u root # sudo 명령어를 통해서 root계정으로 mysql에 접속

1. List of privillages
* select * from mysql.user;
* select host, user, password from mysql.user;

User creation
create user 'megait31'@'%' identified by '비번' //웹사이트를 통해서 접속하기 때문에 이렇게 만들어줘야함.

// control할수 있게 연설시켜주는 것 - GRANT

GRANT all privileges on megait31.* to 'megait31'@'%';

이 예제 에서는 megait31에게 megait31 DB에 access 할수 있는 권한을 준다.

//새로고짐
flush privilages;


mysql user megait31 -privilages

 

2. Create/ Remove Table operation

ALTER - 수정

CREATE - 생성

DROP - 삭제


Create table 
create table board(
    idx int(11) not null auto_increment,
    title varchar(200) not null default '',
    name varchar(20) not null default '',
    contents text not null default '',
    reg_dt datetime not null default '2022-07-31',
    mod_dt datetime not null default '2022-07-31',
    primary key (idx)
);

Table 정보 확인 desc board;

 

Table 수정 (ALTER operation)

//table 수정
alter table board add email varchar(50) not null default '' after name;
//칼럼 이름 수정
alter table board add test varchar(50) not null default '' after member_name;
//칼럼 이름 삭제
alter table board drop column test;
//칼럼의 속성 수정
alter table board modify email varchar(100) not null default '';

//alter -> table 수정 operation

alter table board auto_increment=1;

 

Table data operation 정보를 다루는 문단들… crud
SELECT (r)
INSERT (c)
UPDATE (u)
DELETE (d)

insert into board(title, member_name, email, contents, reg_dt, mod_dt) values  ('hello, world','kim', 'kkk@kkk.com', 'test', now(), now());

-> now() 현재 시간을 표출하는 sql 함수.

 

update board set title=‘hi_world’; -> 모든 열의 title field가 ‘hi_world’로 바뀌므로 where을 꼭 써야함.

update board set titile='hi_world' WHERE idx=2;

 

//drop sepcific row
alter table board2 drop member_name;
alter table board2 drop email;

 

//Insert new row next to (after) specified coloum (idx in this case)


Alter table board2 add member_idx int(11) not null after idx;

 

-------------------

Join operation
insert into member(id, pwd, email, phone, reg_dt, mod_dt) values (‘Kim’, ‘1234’, ‘kkk@kkk.com’, ‘01012345678’, now(), now());

insert into member(id, pwd, email, phone, reg_dt, mod_dt) values ('Kim', '1234', 'kkk@kkk.com', '01012345678', now(), now());

Insert into board2 (member_idx, title, contents, reg_dt, mod_dt) values (1, 'hello, World', 'test', now(), now()); 
Insert into board2 (member_idx, title, contents, reg_dt, mod_dt) values (2, 'hello2, World', 'test', now(), now());

//Join operation으로 두개의 테이블을 조인해서 보여주기-
select * from board2 join member on board2.member_idx = member.idx where board2.idx=1;

 

 

'SQL' 카테고리의 다른 글

JPQLQUery에서의 Fetch Join  (0) 2023.04.04
Limit / offset  (0) 2023.02.03
MySql user info  (0) 2021.08.14