table생성 및 데이터 삽입 create
- 삽입할 칼럼명과 칼럼속성을 나열
- 마지막에 기본키, 연결키를 지정 (추후 add로 지정가능)
- 단, auto_increment를 사용하는 경우 기본키 설정 필수!
create table 테이블명 (
칼럼명1 칼럼속성1,
칼럼명2 칼럼속성2,
...
primary key(칼럼명)
foreign key(연결칼럼명1) references 연결테이블명1(연결테이블의 연결키1)
foreign key(연결칼럼명2) references 연결테이블명2(연결테이블의 연결키2)
...
) ;
-- 생성 후 키 지정시
alter table attend add primary key(기본키) ;
alter table attend add foreign key(연결키) references 타 테이블명(타 테이블 연결키);
-- ezen 사용 (table을 생성할 DB)
mysql> use ezen;
Database changed
-- table 생성
mysql> create table student(
-> num int not null,
-> name varchar(30) not null,
-> age int default 20,
-> address varchar(45),
-> major varchar(45),
-> score int,
-> primary key(num));
Query OK, 0 rows affected (0.01 sec)
-- 연결 키 추가
alter table attend add foreign key(연결키) references 타 테이블명(타 테이블 연결키);
table 설정 확인
desc 테이블명 ;
mysql> desc student; //student table의 설정 확인
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| num | int | NO | PRI | NULL | |
| name | varchar(30) | NO | | NULL | |
| age | int | YES | | 20 | |
| address | varchar(45) | YES | | NULL | |
| major | varchar(45) | YES | | NULL | |
| score | int | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
table에 데이터 입력
- 값이 문자인 경우 ' ' / " " 따옴표 사용 (숫자는 사용X)
- 전체는 *
-- 전체 속성을 넣는 경우
insert into 테이블명 * values
(값1, 값2, 값3..) ;
-- 선택해서 속성의 값만 넣는 경우
insert into 테이블명 (속성1, 속성2, 속성3..) values
(값1, 값2, 값3..) ;
-- 타 테이블에서 값을 가져와 넣는 경우
(전체)
insert into 테이블 명 (속성1, 속성2..)
select * from 타 테이블 명 where 타테이블의 pk;
(선택)
insert into 테이블 명 (속성1, 속성2..)
select 속성1의 값, 속성2의 값.. from 타테이블 명 where 타테이블의 pk;
-- 전체 값 삽입
mysql> insert into student values(5555, '유관순', 21, 'suwon', 'computer', 97);
-- 타 테이블의 모든 값 삽입
mysql> insert into student_4(num, name, age, address, major, score, grade)
-> select * from student where grade = 4;
-- 타 테이블에서 원하는 값만 가져와 삽입
(product_name, price = product테이블에서 가져오기)
mysql> insert into buy(customer, product_name, price, amount)
-> select '동글님', name, price, 3 from product
-> where num = 2;
table 데이터 확인
- 원하는 데이터만 확인하는 경우 , 로 선택 출력
- 전체 데이터를 확인하는 경우 * 사용
-- 전체 출력
select * from 테이블명 ;
-- 선택 출력
select 칼럼명 as "목록명" , 칼럼명2 as "목록명2" from 테이블명 ;
-- 조건이 있는 출력
select * from 테이블명
where 조건 ;
mysql> select*from student;
+------+--------+------+---------+----------+-------+
| num | name | age | address | major | score |
+------+--------+------+---------+----------+-------+
| 1111 | 홍길동 | 23 | seoul | computer | 89 |
| 2222 | 강길순 | 24 | seoul | English | 87 |
| 3333 | 이순신 | 22 | Inchen | English | 57 |
| 4444 | 강감찬 | 23 | Inchen | English | 67 |
| 5555 | 유관순 | 21 | suwon | Computer | 97 |
+------+--------+------+---------+----------+-------+
5 rows in set (0.00 sec)
'DB > 명령프롬프트 - mysql' 카테고리의 다른 글
[MySQL] 프로시저 procedure (함수) (0) | 2023.03.31 |
---|---|
[MySQL] trigger 생성 예제 : update after, old, new (0) | 2023.03.31 |
[MySQL] trigger 생성 예제 : after insert, new (함수사용X) (0) | 2023.03.30 |
[MySQL] 트리거 trigger 예제 : before delete, old 사용 (0) | 2023.03.30 |
[MySQL] 트리거 trigger 예제 : after insert, new 사용 (0) | 2023.03.30 |