delimiter $$
create procedure 스토어드_프로시저_이름 ( in 또는 out 매개변수 )
begin
# SQL 프로그래밍 코드 작성
end $$
delimiter ;
call 스토어드_프로시저_이름(매개변수);
drop procedure user_proc;
delimiter $$
create function 스토어드_함수_이름(매개변수)
returns 반환형식
begin
이 부분에 프로그래밍 코딩
return 반환값;
end $$
delimiter ;
select 스토어드_함수_이름();
set global log_bin_trust_function_creators = 1;
drop function calcYearFunc;
create table if not exists trigger_table (id int, txt varchar(10));
insert into trigger_table values(1, '레드벨벳');
insert into trigger_table values(2, '잇지');
insert into trigger_table values(3, '블랙핑크');
drop trigger if exists myTrigger;
delimiter $$
create trigger myTrigger
after delete
on trigger_table
for each row
begin
set @msg = '가수 그룹이 삭제됨'; -- 트리거 실행 시 작동되는 코드들
end $$
delimiter ;
delete from trigger_table where id = 4;
select @msg;
+----------------------------+
| @msg |
+----------------------------+
| 가수 그룹이 삭제됨 |
+----------------------------+
1 row in set (0.00 sec)
create table singer (select mem_id, mem_name, mem_number, addr from member);
create table backup_singer(
mem_id char(8) not null ,
mem_name varchar(10) not null,
mem_number int not null,
addr char(2) not null ,
modType char(2), -- 변경된 타입. '수정' 또는 '삭제'
modDate date, -- 변경된 날짜
modUser varchar(30) -- 변경한 사용자
);
drop trigger if exists singer_updateTrg;
delimiter $$
create trigger singer_updateTrg -- 트리거 이름
after update -- 변경 후에 작동하도록 지정
on singer -- 트리거를 부착할 테이블
for each row
begin
insert into backup_singer values
(OLD.mem_id, OLD.mem_name, OLD.mem_number, OLD.addr, '수정', curdate(), current_user());
end $$
delimiter ;
drop trigger if exists singer_deleteTrg;
delimiter $$
create trigger singer_deleteTrg -- 트리거 이름
after delete -- 변경 후에 작동하도록 지정
on singer -- 트리거를 부착할 테이블
for each row
begin
insert into backup_singer values
(OLD.mem_id, OLD.mem_name, OLD.mem_number, OLD.addr, '삭제', curdate(), current_user());
end $$
delimiter ;
update singer set addr = '영국' where mem_id = 'BLK';
delete from singer where mem_number >= 7;
select * from backup_singer;
+--------+--------------+------------+--------+---------+------------+----------------+
| mem_id | mem_name | mem_number | addr | modType | modDate | modUser |
+--------+--------------+------------+--------+---------+------------+----------------+
| BLK | 블랙핑크 | 4 | 경남 | 수정 | 2023-08-20 | root@localhost |
| GRL | 소녀시대 | 8 | 서울 | 삭제 | 2023-08-20 | root@localhost |
| OMY | 오마이걸 | 7 | 서울 | 삭제 | 2023-08-20 | root@localhost |
| SPC | 우주소녀 | 13 | 서울 | 삭제 | 2023-08-20 | root@localhost |
| TWC | 트와이스 | 9 | 서울 | 삭제 | 2023-08-20 | root@localhost |
+--------+--------------+------------+--------+---------+------------+----------------+
5 rows in set (0.00 sec)
truncate table singer;
select * from backup_singer;
+--------+--------------+------------+--------+---------+------------+----------------+
| mem_id | mem_name | mem_number | addr | modType | modDate | modUser |
+--------+--------------+------------+--------+---------+------------+----------------+
| BLK | 블랙핑크 | 4 | 경남 | 수정 | 2023-08-20 | root@localhost |
| GRL | 소녀시대 | 8 | 서울 | 삭제 | 2023-08-20 | root@localhost |
| OMY | 오마이걸 | 7 | 서울 | 삭제 | 2023-08-20 | root@localhost |
| SPC | 우주소녀 | 13 | 서울 | 삭제 | 2023-08-20 | root@localhost |
| TWC | 트와이스 | 9 | 서울 | 삭제 | 2023-08-20 | root@localhost |
+--------+--------------+------------+--------+---------+------------+----------------+
5 rows in set (0.00 sec)
import pymysql
from tkinter import *
from tkinter import messagebox
def insertData():
con, cur = None, None
data1, data2, data3, data4 = "", "", "", ""
sql = ""
conn = pymysql.connect(host='127.0.0.1', user='root', password='', db='soloDB', charset='utf8')
cur = conn.cursor()
data1 = edt1.get();
data2 = edt2.get();
data3 = edt3.get();
data4 = edt4.get()
sql = "INSERT INTO userTable VALUES('" + data1 + "','" + data2 + "','" + data3 + "'," + data4 + ")"
cur.execute(sql)
conn.commit()
conn.close()
messagebox.showinfo('성공', '데이터 입력 성공')
def selectData():
strData1, strData2, strData3, strData4 = [], [], [], []
conn = pymysql.connect(host='127.0.0.1', user='root', password='', db='soloDB', charset='utf8')
cur = conn.cursor()
cur.execute("SELECT * FROM userTable")
strData1.append("사용자 ID")
strData2.append("사용자 이름")
strData3.append("사용자 이메일")
strData4.append("사용자 출생연도")
strData1.append("-----------")
strData2.append("-----------")
strData3.append("-----------")
strData4.append("-----------")
while (True):
row = cur.fetchone()
if row == None:
break
strData1.append(row[0])
strData2.append(row[1])
strData3.append(row[2])
strData4.append(row[3])
listData1.delete(0, listData1.size() - 1)
listData2.delete(0, listData2.size() - 1)
listData3.delete(0, listData3.size() - 1)
listData4.delete(0, listData4.size() - 1)
for item1, item2, item3, item4 in zip(strData1, strData2, strData3, strData4):
listData1.insert(END, item1)
listData2.insert(END, item2)
listData3.insert(END, item3)
listData4.insert(END, item4)
conn.close()
root = Tk()
root.geometry("600x300")
root.title("완전한 GUI 응용 프로그램")
edtFrame = Frame(root)
edtFrame.pack()
listFrame = Frame(root)
listFrame.pack(side=BOTTOM, fill=BOTH, expand=1)
edt1 = Entry(edtFrame, width=10)
edt1.pack(side=LEFT, padx=10, pady=10)
edt2 = Entry(edtFrame, width=10)
edt2.pack(side=LEFT, padx=10, pady=10)
edt3 = Entry(edtFrame, width=10)
edt3.pack(side=LEFT, padx=10, pady=10)
edt4 = Entry(edtFrame, width=10)
edt4.pack(side=LEFT, padx=10, pady=10)
btnInsert = Button(edtFrame, text="입력", command=insertData)
btnInsert.pack(side=LEFT, padx=10, pady=10)
btnSelect = Button(edtFrame, text="조회", command=selectData)
btnSelect.pack(side=LEFT, padx=10, pady=10)
listData1 = Listbox(listFrame, bg='black')
listData1.pack(side=LEFT, fill=BOTH, expand=1)
listData2 = Listbox(listFrame, bg='black')
listData2.pack(side=LEFT, fill=BOTH, expand=1)
listData3 = Listbox(listFrame, bg='black')
listData3.pack(side=LEFT, fill=BOTH, expand=1)
listData4 = Listbox(listFrame, bg='black')
listData4.pack(side=LEFT, fill=BOTH, expand=1)
root.mainloop()