DB/명령프롬프트 - mysql 예시

[MySQL] ERD 수강관리 프로그램 1. 생성 (속성)

congs 2023. 3. 28. 14:17

✔ table생성

mysql> create database school;
Query OK, 1 row affected (0.01 sec)

mysql> use school;
Database changed

mysql> create table student(
    -> std_num varchar(10),
    -> std_name varchar(20) not null,
    -> std_major varchar(20),
    -> std_term int,
    -> std_point int,
    -> primary key(std_num));
Query OK, 0 rows affected (0.01 sec)

mysql> desc student;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| std_num   | varchar(10) | NO   | PRI | NULL    |       |
| std_name  | varchar(20) | NO   |     | NULL    |       |
| std_major | varchar(20) | YES  |     | NULL    |       |
| std_term  | int         | YES  |     | NULL    |       |
| std_point | int         | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)

mysql> create table course(
    -> co_code varchar(12),
    -> co_name varchar(20) not null,
    -> co_professor varchar(20),
    -> co_point int,
    -> co_time int,
    -> co_timetable varchar(40),
    -> primary key(co_code));
Query OK, 0 rows affected (0.01 sec)

mysql> desc course;
+--------------+-------------+------+-----+---------+-------+
| Field        | Type        | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| co_code      | varchar(12) | NO   | PRI | NULL    |       |
| co_name      | varchar(20) | NO   |     | NULL    |       |
| co_professor | varchar(20) | YES  |     | NULL    |       |
| co_point     | int         | YES  |     | NULL    |       |
| co_time      | int         | YES  |     | NULL    |       |
| co_timetable | varchar(40) | YES  |     | NULL    |       |
+--------------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

mysql> create table attend(
    -> at_num int auto_increment,
    -> at_std_num varchar(10),
    -> at_co_code varchar(12),
    -> at_year int,
    -> at_term int,
    -> at_mid int default 0,
    -> at_final int default 0,
    -> at_attend int default 0,
    -> at_hw int default 0,
    -> at_repetition varchar(1) default 'n',
    -> at_score varchar(4),
    -> primary key(at_num)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> desc attend;
+---------------+-------------+------+-----+---------+----------------+
| Field         | Type        | Null | Key | Default | Extra          |
+---------------+-------------+------+-----+---------+----------------+
| at_num        | int         | NO   | PRI | NULL    | auto_increment |
| at_std_num    | varchar(10) | YES  |     | NULL    |                |
| at_co_code    | varchar(12) | YES  |     | NULL    |                |
| at_year       | int         | YES  |     | NULL    |                |
| at_term       | int         | YES  |     | NULL    |                |
| at_mid        | int         | YES  |     | 0       |                |
| at_final      | int         | YES  |     | 0       |                |
| at_attend     | int         | YES  |     | 0       |                |
| at_hw         | int         | YES  |     | 0       |                |
| at_repetition | varchar(1)  | YES  |     | n       |                |
| at_score      | varchar(4)  | YES  |     | NULL    |                |
+---------------+-------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)

mysql> alter table attend add foreign key(at_std_num) references student(std_num);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table attend add foreign key(at_co_code) references course(co_code);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc attend;
+---------------+-------------+------+-----+---------+----------------+
| Field         | Type        | Null | Key | Default | Extra          |
+---------------+-------------+------+-----+---------+----------------+
| at_num        | int         | NO   | PRI | NULL    | auto_increment |
| at_std_num    | varchar(10) | YES  | MUL | NULL    |                |
| at_co_code    | varchar(12) | YES  | MUL | NULL    |                |
| at_year       | int         | YES  |     | NULL    |                |
| at_term       | int         | YES  |     | NULL    |                |
| at_mid        | int         | YES  |     | 0       |                |
| at_final      | int         | YES  |     | 0       |                |
| at_attend     | int         | YES  |     | 0       |                |
| at_hw         | int         | YES  |     | 0       |                |
| at_repetition | varchar(1)  | YES  |     | n       |                |
| at_score      | varchar(4)  | YES  |     | NULL    |                |
+---------------+-------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)