Presentation is loading. Please wait.

Presentation is loading. Please wait.

제 2 장 SQL 활용.  SQL 활용 예제 (1) create table student ( id int not null, name char(20) not null, age int, height int, weight float, primary key(id) ); insert.

Similar presentations


Presentation on theme: "제 2 장 SQL 활용.  SQL 활용 예제 (1) create table student ( id int not null, name char(20) not null, age int, height int, weight float, primary key(id) ); insert."— Presentation transcript:

1 제 2 장 SQL 활용

2  SQL 활용 예제 (1) create table student ( id int not null, name char(20) not null, age int, height int, weight float, primary key(id) ); insert into student values(10, ‘ Kim', 40, 170, 68); insert into student values(20, ‘ Lee', 30, 175, 74); insert into student values(30, 'Park', 40, 165, 58); insert into student values(40, 'Choi', 50, 184, 87); select * from student;

3  SQL 활용 예제 (1) insert into student values(40, 'Chung', 50, 154, null); //error insert into student values(50, 20, ‘ Chung', 154, null); //error insert into student values(50, 'Chung', 30, 154, null); insert into student values(null, 'Lim', 50, 162, null); //error select * from student; idnameageheightweight 10Kim4017068 20Lee3017574 30Park4016558 40Choi5018487

4  SQL 활용 예제 (1) select name, age from student where height >= 170 and weight <= 80; select max(height), avg(height), min(height) from student; select age, avg(height), avg(weight) from student group by age; idnameageheightweight 10Kim4017068 20Lee3017574 30Park4016558 40Choi5018487

5  SQL 활용 예제 (1) update student set age = 45, height = 165 where id = 30; delete from student where age <= 40; delete from student; select * from student; drop table student; select * from student; idnameageheightweight 10Kim4017068 20Lee3017574 30Park4016558 40Choi5018487

6  SQL 활용 예제 (2) create table dept ( dno int not null, dname char(20) not null, dname char(20) not null, mgreno int, mgreno int, mgrstartdate char(10), mgrstartdate char(10), primary key(dno) ) ; primary key(dno) ) ; create table emp ( eno int not null, ename char(20) not null, ename char(20) not null, sex char(1), sex char(1), salary int, salary int, dno int, dno int, primary key(eno), primary key(eno), foreign key(dno) references dept(dno) ); foreign key(dno) references dept(dno) );

7  SQL 활용 예제 (2) select ename, salary, dno from emp select ename, salary from emp where salary >=20000 select ename, salary from emp where salary >=20000 and dno = 2 select dno from dept where dname = 'Research'

8  SQL 활용 예제 (2) select dname, ename from emp, dept where mgreno = eno select ename, dname from emp, dept where emp.dno = dept.dno and dname='Research‘ select ename, salary from emp where dno in (select dno from dept where dname = 'Research') select ename, salary from emp where exists (select * from dept where dno = emp.dno and dname = 'Research')

9  SQL 활용 예제 (2) update emp set dno = 5 where eno = 100; // 참조 무결성 위반 delete from dept where dno = 1; // 참조 무결성 위반 update dept set dno = 6 where dno = 1; // 참조 무결성 위반

10  SQL 활용 예제 (2) create table emp ( eno int not null, ename char(20) not null, ename char(20) not null, sex char(1), sex char(1), salary int, salary int, dno int, dno int, primary key(eno), primary key(eno), foreign key(dno) references dept(dno) foreign key(dno) references dept(dno) on delete set null on update cascade); on delete set null on update cascade);

11  SQL 활용 예제 (2) update emp set dno = 5 where eno = 100; // 참조 무결성 위반 delete from dept where dno = 1; //emp 의 dno 가 1 이 null 바뀜 update dept set dno = 6 where dno = 1; // emp 의 dno 가 1 이 6 으로 바뀜


Download ppt "제 2 장 SQL 활용.  SQL 활용 예제 (1) create table student ( id int not null, name char(20) not null, age int, height int, weight float, primary key(id) ); insert."

Similar presentations


Ads by Google