Sie sind auf Seite 1von 10

CREATE VIEW IN MYSQL

mysql> create database viewdb;


Query OK, 1 row affected (0.01 sec)

mysql> use viewdb;


Database changed
mysql> CREATE TABLE product (proid int, proname char(15),qty INT, price INT);
Query OK, 0 rows affected (0.93 sec)

mysql> insert into product values(111,'P1',50,250);


Query OK, 1 row affected (0.20 sec)

mysql> insert into product values(112,'P2',150,25);


Query OK, 1 row affected (0.02 sec)

mysql> insert into product values(113,'P3',75,100);


Query OK, 1 row affected (0.03 sec)

mysql> insert into product values(114,'P4',105,350);


Query OK, 1 row affected (0.02 sec)

mysql> insert into product values(115,'P5',150,120);


Query OK, 1 row affected (0.02 sec)
mysql> SELECT * FROM PRODUCT;
+-------+---------+------+-------+
| proid | proname | qty | price |
+-------+---------+------+-------+
| 111 | P1 | 50 | 250 |
| 112 | P2 | 150 | 25 |
| 113 | P3 | 75 | 100 |
| 114 | P4 | 105 | 350 |
| 115 | P5 | 150 | 120 |
+-------+---------+------+-------+
5 rows in set (0.08 sec)

mysql> CREATE VIEW V1 AS SELECT qty, price, qty*price AS value FROM PRODUCT;
Query OK, 0 rows affected (0.04 sec)

mysql> SELECT * FROM V1;


+------+-------+-------+
| qty | price | value |
+------+-------+-------+
| 50 | 250 | 12500 |
| 150 | 25 | 3750 |
| 75 | 100 | 7500 |
| 105 | 350 | 36750 |
| 150 | 120 | 18000 |
+------+-------+-------+
5 rows in set (0.00 sec)
IMPLEMENTATION OF STRING OPERATIONS AND SORTING
mysql> use empdb;
Database changed
mysql> CREATE TABLE emp (
-> empno decimal(4,0) NOT NULL,
-> ename varchar(10) default NULL,
-> job varchar(9) default NULL,
-> mgr decimal(4,0) default NULL,
-> hiredate date default NULL,
-> sal decimal(7,2) default NULL,
-> comm decimal(7,2) default NULL,
-> deptno decimal(2,0) default NULL
-> );
Query OK, 0 rows affected (0.13 sec)

mysql> INSERT INTO emp VALUES ('7369','SMITH','CLERK','7902','1980-12-17',


'800.00',NULL,'20');
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO emp VALUES ('7499','ALLEN','SALESMAN','7698','1981-02-20',


'1600.00','300.00','30');
Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO emp VALUES ('7521','WARD','SALESMAN','7698','1981-02-22',


'1250.00','500.00','30');
Query OK, 1 row affected (0.03 sec)

mysql> INSERT INTO emp VALUES ('7566','JONES','MANAGER','7839','1981-04-02',


'2975.00',NULL,'20');
Query OK, 1 row affected (0.03 sec)
mysql> INSERT INTO emp VALUES ('7654','MARTIN','SALESMAN','7698','1981-09-28',
'1250.00','1400.00','30');
Query OK, 1 row affected (0.03 sec)

mysql> INSERT INTO emp VALUES ('7698','BLAKE','MANAGER','7839','1981-05-01',


'2850.00',NULL,'30');
Query OK, 1 row affected (0.03 sec)

mysql> INSERT INTO emp VALUES ('7782','CLARK','MANAGER','7839','1981-06-09',


'2450.00',NULL,'10');
Query OK, 1 row affected (0.02 sec)

mysql> INSERT INTO emp VALUES ('7788','SCOTT','ANALYST','7566','1982-12-09',


'3000.00',NULL,'20');
Query OK, 1 row affected (0.04 sec)

mysql> INSERT INTO emp VALUES ('7839','KING','PRESIDENT',NULL,'1981-11-17',


'5000.00',NULL,'10');
Query OK, 1 row affected (0.02 sec)

mysql> INSERT INTO emp VALUES ('7844','TURNER','SALESMAN','7698','1981-09-08',


'1500.00','0.00','30');
Query OK, 1 row affected (0.04 sec)

mysql> INSERT INTO emp VALUES ('7876','ADAMS','CLERK','7788','1983-01-12',


'1100.00',NULL,'20');
Query OK, 1 row affected (0.02 sec)

mysql> INSERT INTO emp VALUES ('7900','JAMES','CLERK','7698','1981-12-03',


'950.00',NULL,'30');
Query OK, 1 row affected (0.02 sec)
mysql> SELECT * FROM EMP;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7369 SMITH CLERK 7902 17-DEC-80 800 20

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7566 JONES MANAGER 7839 02-APR-81 2975 20

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7698 BLAKE MANAGER 7839 01-MAY-81 2850 30

7782 CLARK MANAGER 7839 09-JUN-81 2450 10

7788 SCOTT ANALYST 7566 19-APR-87 3000 20

7839 KING PRESIDENT 17-NOV-81 5000 10

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7876 ADAMS CLERK 7788 23-MAY-87 1100 20

7900 JAMES CLERK 7698 03-DEC-81 950 30

7902 FORD ANALYST 7566 03-DEC-81 3000 20

7934 MILLER CLERK 7782 23-JAN-82 1300 10

12 rows selected.

mysql> select empno,ename,job from emp where ename like '%S';


+-------+-------+---------+
| empno | ename | job |
+-------+-------+---------+
| 7566 | JONES | MANAGER |
| 7876 | ADAMS | CLERK |
| 7900 | JAMES | CLERK |
+-------+-------+---------+
3 rows in set (0.06 sec)
mysql> select empno,ename,job from emp where ename like 'S%';
+-------+-------+---------+
| empno | ename | job |
+-------+-------+---------+
| 7369 | SMITH | CLERK |
| 7788 | SCOTT | ANALYST |
+-------+-------+---------+
2 rows in set (0.00 sec)

mysql> select ename,sal from emp order by sal;


+--------+---------+
| ename | sal |
+--------+---------+
| SMITH | 800.00 |
| JAMES | 950.00 |
| ADAMS | 1100.00 |
| WARD | 1250.00 |
| MARTIN | 1250.00 |
| TURNER | 1500.00 |
| ALLEN | 1600.00 |
| CLARK | 2450.00 |
| BLAKE | 2850.00 |
| JONES | 2975.00 |
| SCOTT | 3000.00 |
| KING | 5000.00 |
+--------+---------+
12 rows in set (0.03 sec)
mysql> select ename,sal from emp order by sal ASC;
+--------+---------+
| ename | sal |
+--------+---------+
| SMITH | 800.00 |
| JAMES | 950.00 |
| ADAMS | 1100.00 |
| WARD | 1250.00 |
| MARTIN | 1250.00 |
| TURNER | 1500.00 |
| ALLEN | 1600.00 |
| CLARK | 2450.00 |
| BLAKE | 2850.00 |
| JONES | 2975.00 |
| SCOTT | 3000.00 |
| KING | 5000.00 |
+--------+---------+
12 rows in set (0.00 sec)

mysql> select ename,sal from emp order by sal DESC;


+--------+---------+
| ename | sal |
+--------+---------+
| KING | 5000.00 |
| SCOTT | 3000.00 |
| JONES | 2975.00 |
| BLAKE | 2850.00 |
| CLARK | 2450.00 |
| ALLEN | 1600.00 |
| TURNER | 1500.00 |
| MARTIN | 1250.00 |
| WARD | 1250.00 |
| ADAMS | 1100.00 |
| JAMES | 950.00 |
| SMITH | 800.00 |
+--------+---------+
12 rows in set (0.00 sec)

mysql> select ename,sal from emp order by ename ASC;


+--------+---------+
| ename | sal |
+--------+---------+
| ADAMS | 1100.00 |
| ALLEN | 1600.00 |
| BLAKE | 2850.00 |
| CLARK | 2450.00 |
| JAMES | 950.00 |
| JONES | 2975.00 |
| KING | 5000.00 |
| MARTIN | 1250.00 |
| SCOTT | 3000.00 |
| SMITH | 800.00 |
| TURNER | 1500.00 |
| WARD | 1250.00 |
+--------+---------+
12 rows in set (0.00 sec)
mysql> select ename,sal from emp order by ename DESC;
+--------+---------+
| ename | sal |
+--------+---------+
| WARD | 1250.00 |
| TURNER | 1500.00 |
| SMITH | 800.00 |
| SCOTT | 3000.00 |
| MARTIN | 1250.00 |
| KING | 5000.00 |
| JONES | 2975.00 |
| JAMES | 950.00 |
| CLARK | 2450.00 |
| BLAKE | 2850.00 |
| ALLEN | 1600.00 |
| ADAMS | 1100.00 |
+--------+---------+
12 rows in set (0.00 sec)

mysql> select empno,ename,job from emp where ename like 'S___H';


+-------+-------+-------+
| empno | ename | job |
+-------+-------+-------+
| 7369 | SMITH | CLERK |
+-------+-------+-------+
1 row in set (0.00 sec)
mysql> select empno,ename,job from emp where ename like 'J___S';
+-------+-------+---------+
| empno | ename | job |
+-------+-------+---------+
| 7566 | JONES | MANAGER |
| 7900 | JAMES | CLERK |
+-------+-------+---------+
2 rows in set (0.00 sec)

mysql> select empno,ename,job from emp where ename like '_LA__';


+-------+-------+---------+
| empno | ename | job |
+-------+-------+---------+
| 7698 | BLAKE | MANAGER |
| 7782 | CLARK | MANAGER |
+-------+-------+---------+
2 rows in set (0.00 sec)