λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

STUDY/DATABASE

My SQL ν…Œμ΄λΈ”μ˜ 데이터 μ‘°μž‘(auto_incremenet, insert)

λ°˜μ‘ν˜•

AUTO_INCREMENT?

 ν…Œμ΄λΈ” 생성(μˆ˜μ •) μ‹œ auto_increment 속성을 λΆ€μ—¬ν•˜λ©΄ insert μ‹œ μžλ™μœΌλ‘œ 1λΆ€ν„° μ‹œμž‘ν•΄μ„œ 1μ”© μ¦κ°€ν•˜λŠ” 값을 λ°˜ν™˜

 primary key λ˜λŠ” unique μ œμ•½μ‘°κ±΄μ΄ μ§€μ •λœ 컬럼만 ν™œμš© κ°€λŠ₯

 μˆ«μžν˜•μƒ‰μ˜ 데이터 νƒ€μž…μ—λ§Œ ν™œμš© κ°€λŠ₯

 insert μž‘μ—… μ‹œ null 값을 μ§€μ •ν•˜λ©΄ μžλ™μœΌλ‘œ 값이 μž…λ ₯됨

creat table ν…Œμ΄λΈ”λͺ…
( 컬럼1 int auto_increament primary key,
  컬럼2 데이터 νƒ€μž…,
  컬럼3 데이터 νƒ€μž…);
-------------------------------------------------
alter table ν…Œμ΄λΈ”λͺ… auto_increment=μž…λ ₯κ°’; > μ‹œμž‘κ°’ λ³€κ²½(default =1)

set @@auto_increment_increment=증가값; > 증가값 λ³€κ²½(default =1)
-------------------------------------------------
select last_insert_id(); > ν˜„μž¬ μ‚¬μš© 번호 확인

 

1. ν…Œμ΄λΈ” 생성 μ‹œ AUTO_INCREMENT 속성 ν™œμš©

create table stu20
(stu_id int auto_increment primary key,
 stu_name varchar(5) not null,
 age int check(age>19) );
 
 desc stu20;
alter table stu20 auto_increment=100;
//λ‹€μŒ μž…λ ₯값이 100으둜 λ³€κ²½

insert into stu20
values(null, 'κΉ€00', 29);
//κΉ€00의 stu_idκ°€ 100 λΆ€ν„° μ‹œμž‘ν•¨

insert into stu20
values(null, '졜00' 3);
//증가값은 κ·ΈλŒ€λ‘œμ΄λ―€λ‘œ 졜00의 stu_idλŠ” 101

set @@auto_increment_increment=5;
//증가값이 5둜 λ³€κ²½

insert into stu20
values(null, 'λ°•00',22);
//증가값이 5둜 λ³€κ²½ λ¬μœΌλ―€λ‘œ λ°•00의 stu_idλŠ” 106

데이터 μ‘°μž‘μ–΄(DML)

  1. insert : μ‚½μž…
  2. update : μˆ˜μ •
  3. delete : μ‚­μ œ

2. 데이터 μ‚½μž… : INSERT

   * 데이터 μ‚½μž… μ‹œ μ œμ•½μ‘°κ±΄ 유의

   * 문자 및 λ‚ μ§œλŠ” μž‘μ€ λ”°μ˜΄ν‘œλ‘œ λ¬Άμ–΄μ„œ ν‘œν˜„

   * λ‚ μ§œ : 년도-μ›”-일 μˆœμ„œλ‘œ μž‘μ„±

insert into members
values(100, '홍길동', '1991-12-30', '학생', '010-1111-1111', 'λΆ€μ‚° 뢀산진ꡬ 00동');

    μƒλž΅λœ μ»¬λŸΌμ—λŠ” null 값이 μžλ™ μ‚½μž…

 

3. Error Code : 1364. Field 'birth' doesn't have a default value

 -> not null μ œμ•½μ‘°κ±΄μ΄ μ„ μ–Έλœ μ»¬λŸΌμ— 값을 μƒλž΅ν•œ 경우

 

4. Error Code : 1062. Duplicate entry '010-0000-0000' for key 'phone'

 -> unique μ œμ•½μ‘°κ±΄μ΄ μ„ μ–Έλœ μ»¬λŸΌμ— 쀑볡 값이 μ‚½μž…λœ 경우

 

5. Error Code : 1062. Duplicate entry '101' for key 'PRIMARY'

 -> primary key μ œμ•½μ‘°κ±΄μ΄ μ„ μ–Έλœ μ»¬λŸΌμ— 쀑볡 값이 μ‚½μž…λœ 경우

 

6. Error Code : 1364. Field 'member_id' doesn't have a default value

 -> primary key μ œμ•½μ‘°κ±΄μ΄ μ„ μ–Έλœ μ»¬λŸΌμ— 값을 μƒλž΅ν•œ 경우

 

 

λ°˜μ‘ν˜•

'STUDY > DATABASE' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

MySQL - ERD λ§Œλ“€κΈ°  (0) 2022.04.12
MySQL 문법  (0) 2022.04.12
MySQL 데이터 쑰회(SELECT)  (0) 2022.04.11
MySQL 데이터 μ‘°μž‘(UPDATE, DELETE)  (0) 2022.04.11
MySQL DB생성 및 ν…Œμ΄λΈ” 생성  (0) 2022.04.11