Sie sind auf Seite 1von 15

mysql> use posyandu2;

Database changed
mysql> show tables;
+---------------------+
| Tables_in_posyandu2 |
+---------------------+
| anggkk
|
| balita
|
| kk
|
| lacak
|
| pemulihan
|
| pmt
|
| timbang
|
| vitamin
|
+---------------------+
8 rows in set (0.00 sec)
mysql> create table job(job_id int(4) Primary KEy, title char(20));
Query OK, 0 rows affected (2.05 sec)
mysql> insert into job values(1,'programmer'),(2,'Tester');
Query OK, 2 rows affected (0.08 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into job values(3,'Manager'),(4,'Spy');
Query OK, 2 rows affected (1.20 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into job values(5,'Detective'),(6,'Forensics');
Query OK, 2 rows affected (1.83 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into job values(7,'Developer');
Query OK, 1 row affected (0.05 sec)
mysql> create table employee(id char(3) primary key,
-> first_name char(10),
-> last_name char(20),
-> start_date date,
-> end_date date,
-> salary float(20),
-> city char(30),
-> jod_id int(3),
-> foreign key(job_id) references job(job_id));
ERROR 1072 (42000): Key column 'job_id' doesn't exist in table
mysql> create table employee(id char(3) primary key,
-> first_name char(10),
-> last_name char(20),
-> start_date date,
-> end_date date,
-> salary float(20),
-> city char(30),
-> job_id int(3),
-> foreign key(job_id) references job(job_id));
Query OK, 0 rows affected (0.14 sec)
mysql> insert into job values (1,'Jason','Martin','1996/07/25','2006/07/25',1235
.56,1);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> insert into job values (1,'Jason','Martin','1996/07/25','2006/07/25',1235

.56,'Toronto',1);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> insert into employee values (1,'Jason','Martin','1996/07/25','2006/07/25'
,1235.56,'Toronto',1);
Query OK, 1 row affected (0.05 sec)
mysql> insert into employee values (2,'Alison','Mathews','1976/03/21','2086/02/2
1',1235/56,'Toronto',2),
-> insert into employee values (2,'Alison','Mathews','1976/03/21','2086/02/2
1',1235/56,'Vancouver',2),
-> \c
mysql> insert into employee values (2,'Alison','Mathews','1976/03/21','1986/02/2
1',6662.78,'Vancouver',2),
-> (3,'James','Smith','1978/12/12','1990/03/15',6545.78,'Vancouver',2);
Query OK, 2 rows affected (0.05 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into employee values (4,'Celia','Rice','1982/10/24','1999/04/21',2
345.78,'Vancouver',3),
-> (5,'Robert','Black','1984/01/12','1998/08/08',2335.78,'Vancouver',2);
Query OK, 2 rows affected (0.63 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into employee values (6,'Linda','Green','1987/07/30','1996/01/04',
4323.78,'New York',3),
-> (7,'David','Larry','1990/12/31','1998/02/12',7898.78,'New York',3);
Query OK, 2 rows affected (1.83 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into employee values (8,'James','Cat','1996/09/17','2002/04/15',12
33.78,'Vancouver',2),
-> (10,'Hercule','Poirot','1973/05/23','2001/08/09',4313.98,'Brussels',5);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into employee values (11,'Lincoln','Rhyme','1999/05/25','2011/07/1
3',3213.98,'New York',6),
-> (12,'Sherlock','Holmes','1923/08/12','1945/07/21',4124.98,'London',5);
Query OK, 2 rows affected (1.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select* from employee;
+----+------------+-----------+------------+------------+---------+-----------+-------+
| id | first_name | last_name | start_date | end_date | salary | city
|
job_id |
+----+------------+-----------+------------+------------+---------+-----------+-------+
| 1 | Jason
| Martin
| 1996-07-25 | 2006-07-25 | 1235.56 | Toronto |
1 |
| 10 | Hercule
| Poirot
| 1973-05-23 | 2001-08-09 | 4313.98 | Brussels |
5 |
| 11 | Lincoln
| Rhyme
| 1999-05-25 | 2011-07-13 | 3213.98 | New York |
6 |
| 12 | Sherlock | Holmes
| 1923-08-12 | 1945-07-21 | 4124.98 | London
|
5 |
| 2 | Alison
| Mathews | 1976-03-21 | 1986-02-21 | 6662.78 | Vancouver |
2 |
| 3 | James
| Smith
| 1978-12-12 | 1990-03-15 | 6545.78 | Vancouver |
2 |

| 4 | Celia
| Rice
| 1982-10-24 | 1999-04-21 | 2345.78 | Vancouver |
3 |
| 5 | Robert
| Black
| 1984-01-12 | 1998-08-08 | 2335.78 | Vancouver |
2 |
| 6 | Linda
| Green
| 1987-07-30 | 1996-01-04 | 4323.78 | New York |
3 |
| 7 | David
| Larry
| 1990-12-31 | 1998-02-12 | 7898.78 | New York |
3 |
| 8 | James
| Cat
| 1996-09-17 | 2002-04-15 | 1233.78 | Vancouver |
2 |
+----+------------+-----------+------------+------------+---------+-----------+-------+
11 rows in set (0.05 sec)
mysql> select* from employee order by employee;
ERROR 1054 (42S22): Unknown column 'employee' in 'order clause'
mysql> select* from employee order by id;
+----+------------+-----------+------------+------------+---------+-----------+-------+
| id | first_name | last_name | start_date | end_date | salary | city
|
job_id |
+----+------------+-----------+------------+------------+---------+-----------+-------+
| 1 | Jason
| Martin
| 1996-07-25 | 2006-07-25 | 1235.56 | Toronto |
1 |
| 10 | Hercule
| Poirot
| 1973-05-23 | 2001-08-09 | 4313.98 | Brussels |
5 |
| 11 | Lincoln
| Rhyme
| 1999-05-25 | 2011-07-13 | 3213.98 | New York |
6 |
| 12 | Sherlock | Holmes
| 1923-08-12 | 1945-07-21 | 4124.98 | London
|
5 |
| 2 | Alison
| Mathews | 1976-03-21 | 1986-02-21 | 6662.78 | Vancouver |
2 |
| 3 | James
| Smith
| 1978-12-12 | 1990-03-15 | 6545.78 | Vancouver |
2 |
| 4 | Celia
| Rice
| 1982-10-24 | 1999-04-21 | 2345.78 | Vancouver |
3 |
| 5 | Robert
| Black
| 1984-01-12 | 1998-08-08 | 2335.78 | Vancouver |
2 |
| 6 | Linda
| Green
| 1987-07-30 | 1996-01-04 | 4323.78 | New York |
3 |
| 7 | David
| Larry
| 1990-12-31 | 1998-02-12 | 7898.78 | New York |
3 |
| 8 | James
| Cat
| 1996-09-17 | 2002-04-15 | 1233.78 | Vancouver |
2 |
+----+------------+-----------+------------+------------+---------+-----------+-------+
11 rows in set (0.00 sec)
mysql> decs emlployee;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version
for the right syntax to use near 'decs emlployee' at line 1
mysql> decs employee;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version
for the right syntax to use near 'decs employee' at line 1
mysql> desc employee;
+------------+----------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |

+------------+----------+------+-----+---------+-------+
| id
| char(3) | NO | PRI |
|
|
| first_name | char(10) | YES |
| NULL
|
|
| last_name | char(20) | YES |
| NULL
|
|
| start_date | date
| YES |
| NULL
|
|
| end_date | date
| YES |
| NULL
|
|
| salary
| float
| YES |
| NULL
|
|
| city
| char(30) | YES |
| NULL
|
|
| job_id
| int(3) | YES | MUL | NULL
|
|
+------------+----------+------+-----+---------+-------+
8 rows in set (0.79 sec)
mysql> alter table employee modify id int;
Query OK, 11 rows affected (1.01 sec)
Records: 11 Duplicates: 0 Warnings: 0
mysql> desc employee;
+------------+----------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+------------+----------+------+-----+---------+-------+
| id
| int(11) | NO | PRI | 0
|
|
| first_name | char(10) | YES |
| NULL
|
|
| last_name | char(20) | YES |
| NULL
|
|
| start_date | date
| YES |
| NULL
|
|
| end_date | date
| YES |
| NULL
|
|
| salary
| float
| YES |
| NULL
|
|
| city
| char(30) | YES |
| NULL
|
|
| job_id
| int(3) | YES | MUL | NULL
|
|
+------------+----------+------+-----+---------+-------+
8 rows in set (0.01 sec)
mysql> select* from employee;
+----+------------+-----------+------------+------------+---------+-----------+-------+
| id | first_name | last_name | start_date | end_date | salary | city
|
job_id |
+----+------------+-----------+------------+------------+---------+-----------+-------+
| 1 | Jason
| Martin
| 1996-07-25 | 2006-07-25 | 1235.56 | Toronto |
1 |
| 2 | Alison
| Mathews | 1976-03-21 | 1986-02-21 | 6662.78 | Vancouver |
2 |
| 3 | James
| Smith
| 1978-12-12 | 1990-03-15 | 6545.78 | Vancouver |
2 |
| 4 | Celia
| Rice
| 1982-10-24 | 1999-04-21 | 2345.78 | Vancouver |
3 |
| 5 | Robert
| Black
| 1984-01-12 | 1998-08-08 | 2335.78 | Vancouver |
2 |
| 6 | Linda
| Green
| 1987-07-30 | 1996-01-04 | 4323.78 | New York |
3 |
| 7 | David
| Larry
| 1990-12-31 | 1998-02-12 | 7898.78 | New York |
3 |
| 8 | James
| Cat
| 1996-09-17 | 2002-04-15 | 1233.78 | Vancouver |
2 |
| 10 | Hercule
| Poirot
| 1973-05-23 | 2001-08-09 | 4313.98 | Brussels |
5 |
| 11 | Lincoln
| Rhyme
| 1999-05-25 | 2011-07-13 | 3213.98 | New York |
6 |
| 12 | Sherlock | Holmes
| 1923-08-12 | 1945-07-21 | 4124.98 | London
|
5 |

+----+------------+-----------+------------+------------+---------+-----------+-------+
11 rows in set (0.00 sec)
mysql> DELIMITER //
mysql> CREATE TRIGGER before_insert BEFORE INSERT ON employee
-> FOR EACH ROW
-> BEGIN
-> IF NEW.salary IS NULL OR NEW.salary=0 THEN
-> SET NEW.salary=1000;
-> else
-> SET NEW.salary= NEW.salary+100;
-> END IF;
-> END //
Query OK, 0 rows affected (0.33 sec)
mysql> DELIMITER ;
mysql> insert into employee values (14,'Lincoln','Rhyme','1999/05/25','2011/07/1
3',null,'New York',6);
Query OK, 1 row affected (1.85 sec)
mysql> select* from employee;
+----+------------+-----------+------------+------------+---------+-----------+-------+
| id | first_name | last_name | start_date | end_date | salary | city
|
job_id |
+----+------------+-----------+------------+------------+---------+-----------+-------+
| 1 | Jason
| Martin
| 1996-07-25 | 2006-07-25 | 1235.56 | Toronto |
1 |
| 2 | Alison
| Mathews | 1976-03-21 | 1986-02-21 | 6662.78 | Vancouver |
2 |
| 3 | James
| Smith
| 1978-12-12 | 1990-03-15 | 6545.78 | Vancouver |
2 |
| 4 | Celia
| Rice
| 1982-10-24 | 1999-04-21 | 2345.78 | Vancouver |
3 |
| 5 | Robert
| Black
| 1984-01-12 | 1998-08-08 | 2335.78 | Vancouver |
2 |
| 6 | Linda
| Green
| 1987-07-30 | 1996-01-04 | 4323.78 | New York |
3 |
| 7 | David
| Larry
| 1990-12-31 | 1998-02-12 | 7898.78 | New York |
3 |
| 8 | James
| Cat
| 1996-09-17 | 2002-04-15 | 1233.78 | Vancouver |
2 |
| 10 | Hercule
| Poirot
| 1973-05-23 | 2001-08-09 | 4313.98 | Brussels |
5 |
| 11 | Lincoln
| Rhyme
| 1999-05-25 | 2011-07-13 | 3213.98 | New York |
6 |
| 12 | Sherlock | Holmes
| 1923-08-12 | 1945-07-21 | 4124.98 | London
|
5 |
| 14 | Lincoln
| Rhyme
| 1999-05-25 | 2011-07-13 |
1000 | New York |
6 |
+----+------------+-----------+------------+------------+---------+-----------+-------+
12 rows in set (0.00 sec)
mysql> create table trans_log(
-> user_id varchar(15),
-> description varchar(50));
Query OK, 0 rows affected (1.85 sec)

mysql> DELIMITER $$
mysql> CREATE TRIGGER log_salary AFTER UPDATE
-> ON employee
-> for EACH ROW
-> BEGIN
->
insert into trans_log
-> values (user(),CONCAT(' merubah akun ',NEW.id,' dari ',OLD.salary,' to ',
New.salary));
-> end $$;
Query OK, 0 rows affected (0.01 sec)
-> DELIMITER ;
-> \C
ERROR:
Usage: \C char_setname | charset charset_name
-> \c
mysql> DELIMITER $$
mysql> CREATE TRIGGER log_salary AFTER UPDATE
-> ON employee
-> for EACH ROW
-> BEGIN
->
insert into trans_log
-> values (user(),CONCAT(' merubah akun ',NEW.id,' dari ',OLD.salary,' to ',
New.salary));
-> end $$;
ERROR 1235 (42000): This version of MySQL doesn't yet support 'multiple triggers
with the same action time and event for
one table'
-> delimiter;
-> DELIMITER ;
-> DELOMITER;
-> DELIMITER;
-> DELIMITER ;
-> DELIMITER ;
-> DELIMITER&&;
-> \C
ERROR:
Usage: \C char_setname | charset charset_name
-> \c
mysql> update emlpoyee SET salary=salary+1000;
-> \C
ERROR:
Usage: \C char_setname | charset charset_name
-> \c]
-> \c
mysql> DELIMITER ;
mysql> update emlpoyee SET salary=salary+1000;
ERROR 1146 (42S02): Table 'posyandu2.emlpoyee' doesn't exist
mysql> update employee SET salary=salary+1000;
Query OK, 12 rows affected (1.85 sec)
Rows matched: 12 Changed: 12 Warnings: 0
mysql> select*FROM trans_log;
+----------------+------------------------------------------+
| user_id
| description
|
+----------------+------------------------------------------+
| root@localhost | merubah akun 1 dari 1235.56 to 2235.56 |
| root@localhost | merubah akun 2 dari 6662.78 to 7662.78 |
| root@localhost | merubah akun 3 dari 6545.78 to 7545.78 |

| root@localhost | merubah akun 4 dari 2345.78 to 3345.78 |


| root@localhost | merubah akun 5 dari 2335.78 to 3335.78 |
| root@localhost | merubah akun 6 dari 4323.78 to 5323.78 |
| root@localhost | merubah akun 7 dari 7898.78 to 8898.78 |
| root@localhost | merubah akun 8 dari 1233.78 to 2233.78 |
| root@localhost | merubah akun 10 dari 4313.98 to 5313.98 |
| root@localhost | merubah akun 11 dari 3213.98 to 4213.98 |
| root@localhost | merubah akun 12 dari 4124.98 to 5124.98 |
| root@localhost | merubah akun 14 dari 1000 to 2000
|
+----------------+------------------------------------------+
12 rows in set (0.00 sec)
mysql> SHOW TRIGGERS IN posyandu2\G
*************************** 1. row ***************************
Trigger: before_insert
Event: INSERT
Table: employee
Statement: BEGIN
IF NEW.salary IS NULL OR NEW.salary=0 THEN
SET NEW.salary=1000;
else
SET NEW.salary= NEW.salary+100;
END IF;
END
Timing: BEFORE
Created: NULL
sql_mode: STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Definer: root@localhost
*************************** 2. row ***************************
Trigger: log_salary
Event: UPDATE
Table: employee
Statement: BEGIN
insert into trans_log
values (user(),CONCAT(' merubah akun ',NEW.id,' dari ',OLD.salary,' to ', New.sa
lary));
end
Timing: AFTER
Created: NULL
sql_mode: STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Definer: root@localhost
2 rows in set (1.39 sec)
mysql> select TRIGGER_NAME,event_object_table
-> FROM INFORMATION_SCHEMA.TRIGGERS;
+---------------+--------------------+
| TRIGGER_NAME | event_object_table |
+---------------+--------------------+
| before_insert | employee
|
| log_salary
| employee
|
+---------------+--------------------+
2 rows in set (1.50 sec)
mysql>
->
->
mysql>
->
->
->

CREATE TABLE EMPLOYEE_BAK(


id int(11) not null primary key,
\c
CREATE TABLE EMPLOYEE_BAK(
id int(11) not null,
first_name varchar(15),
last_name varchar(15),

-> start_date date,


-> end_date date,
-> salary float(8,2),
-> city varchar(10),
-> job_id int(11));
Query OK, 0 rows affected (0.10 sec)
mysql> desc employee_bak;
+------------+-------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id
| int(11)
| NO |
|
|
|
| first_name | varchar(15) | YES |
| NULL
|
|
| last_name | varchar(15) | YES |
| NULL
|
|
| start_date | date
| YES |
| NULL
|
|
| end_date | date
| YES |
| NULL
|
|
| salary
| float(8,2) | YES |
| NULL
|
|
| city
| varchar(10) | YES |
| NULL
|
|
| job_id
| int(11)
| YES |
| NULL
|
|
+------------+-------------+------+-----+---------+-------+
8 rows in set (0.01 sec)
mysql> DELIMITER //
mysql> CREATE TRIGGER employee_bak after DELETE
-> ON employee
-> FOR EACH ROW
-> BEGIN
-> \c
mysql> CREATE TRIGGER hapus_data after DELETE
-> ON employee
-> FOR EACH ROW
-> BEGIN
->
insert into employee_bak values(id,first_name,last_name,start_da
te,end_date,
-> salary,city,job_id);
-> end //
Query OK, 0 rows affected (0.01 sec)
mysql> DELIMITER ;
mysql> delete from employee where id=14;
Query OK, 1 row affected (0.05 sec)
mysql> select*from employee_bak;
+----+------------+-----------+------------+----------+--------+------+--------+
| id | first_name | last_name | start_date | end_date | salary | city | job_id |
+----+------------+-----------+------------+----------+--------+------+--------+
| 0 | NULL
| NULL
| NULL
| NULL
| NULL | NULL | NULL |
+----+------------+-----------+------------+----------+--------+------+--------+
1 row in set (0.00 sec)
mysql> DELIMITER //
mysql> CREATE TRIGGER hapus_data after DELETE
-> ON employee
-> FOR EACH ROW
-> BEGIN
->
insert into employee_bak values(OLD.id,OLD.first_name,OLD.last_n
ame,
-> OLD.start_date,OLD.end_date,OLD.salary,OLD.job_id);
-> end //
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql> delete from employee where id=14;
Query OK, 0 rows affected (0.00 sec)
mysql> delete from employee where id=13;
Query OK, 0 rows affected (0.00 sec)
mysql> select*from employee_bak;
+----+------------+-----------+------------+----------+--------+------+--------+
| id | first_name | last_name | start_date | end_date | salary | city | job_id |
+----+------------+-----------+------------+----------+--------+------+--------+
| 0 | NULL
| NULL
| NULL
| NULL
| NULL | NULL | NULL |
+----+------------+-----------+------------+----------+--------+------+--------+
1 row in set (0.00 sec)
mysql> select*from employee;
+----+------------+-----------+------------+------------+---------+-----------+-------+
| id | first_name | last_name | start_date | end_date | salary | city
|
job_id |
+----+------------+-----------+------------+------------+---------+-----------+-------+
| 1 | Jason
| Martin
| 1996-07-25 | 2006-07-25 | 2235.56 | Toronto |
1 |
| 2 | Alison
| Mathews | 1976-03-21 | 1986-02-21 | 7662.78 | Vancouver |
2 |
| 3 | James
| Smith
| 1978-12-12 | 1990-03-15 | 7545.78 | Vancouver |
2 |
| 4 | Celia
| Rice
| 1982-10-24 | 1999-04-21 | 3345.78 | Vancouver |
3 |
| 5 | Robert
| Black
| 1984-01-12 | 1998-08-08 | 3335.78 | Vancouver |
2 |
| 6 | Linda
| Green
| 1987-07-30 | 1996-01-04 | 5323.78 | New York |
3 |
| 7 | David
| Larry
| 1990-12-31 | 1998-02-12 | 8898.78 | New York |
3 |
| 8 | James
| Cat
| 1996-09-17 | 2002-04-15 | 2233.78 | Vancouver |
2 |
| 10 | Hercule
| Poirot
| 1973-05-23 | 2001-08-09 | 5313.98 | Brussels |
5 |
| 11 | Lincoln
| Rhyme
| 1999-05-25 | 2011-07-13 | 4213.98 | New York |
6 |
| 12 | Sherlock | Holmes
| 1923-08-12 | 1945-07-21 | 5124.98 | London
|
5 |
+----+------------+-----------+------------+------------+---------+-----------+-------+
11 rows in set (0.00 sec)
mysql> delete from employee where id=12;
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql>
mysql>mysql> DELIMITER //
mysql> CREATE TRIGGER hapus_data after DELETE
-> ON employee
-> FOR EACH ROW
-> BEGIN
-> insert into employee_bak values(OLD.id,OLD.first_name,OLD.last_name,
-> OLD.start_date,OLD.end_date,OLD.salary,OLD.city,OLD.job_id);

-> END //
Query OK, 0 rows affected (0.01 sec)
mysql> DELIMITER ;
mysql> delete from employee where id=10;
Query OK, 1 row affected (1.07 sec)
mysql> select*from employee_bak;
+----+------------+-----------+------------+------------+---------+----------+-------+
| id | first_name | last_name | start_date | end_date | salary | city
| j
ob_id |
+----+------------+-----------+------------+------------+---------+----------+-------+
| 0 | NULL
| NULL
| NULL
| NULL
|
NULL | NULL
|
NULL |
| 10 | Hercule
| Poirot
| 1973-05-23 | 2001-08-09 | 5313.98 | Brussels |
5 |
+----+------------+-----------+------------+------------+---------+----------+-------+
2 rows in set (0.00 sec)
mysql> delete from employee where id=11;
Query OK, 1 row affected (0.19 sec)
mysql> select*from employee_bak;
+----+------------+-----------+------------+------------+---------+----------+-------+
| id | first_name | last_name | start_date | end_date | salary | city
| j
ob_id |
+----+------------+-----------+------------+------------+---------+----------+-------+
| 0 | NULL
| NULL
| NULL
| NULL
|
NULL | NULL
|
NULL |
| 10 | Hercule
| Poirot
| 1973-05-23 | 2001-08-09 | 5313.98 | Brussels |
5 |
| 11 | Lincoln
| Rhyme
| 1999-05-25 | 2011-07-13 | 4213.98 | New York |
6 |
+----+------------+-----------+------------+------------+---------+----------+-------+
3 rows in set (0.00 sec)
mysql> select*from employee;
+----+------------+-----------+------------+------------+---------+-----------+-------+
| id | first_name | last_name | start_date | end_date | salary | city
|
job_id |
+----+------------+-----------+------------+------------+---------+-----------+-------+
| 1 | Jason
| Martin
| 1996-07-25 | 2006-07-25 | 2235.56 | Toronto |
1 |
| 2 | Alison
| Mathews | 1976-03-21 | 1986-02-21 | 7662.78 | Vancouver |
2 |
| 3 | James
| Smith
| 1978-12-12 | 1990-03-15 | 7545.78 | Vancouver |
2 |
| 4 | Celia
| Rice
| 1982-10-24 | 1999-04-21 | 3345.78 | Vancouver |
3 |
| 5 | Robert
| Black
| 1984-01-12 | 1998-08-08 | 3335.78 | Vancouver |
2 |
| 6 | Linda
| Green
| 1987-07-30 | 1996-01-04 | 5323.78 | New York |

3 |
| 7 | David
| Larry
| 1990-12-31 | 1998-02-12 | 8898.78 | New York |
3 |
| 8 | James
| Cat
| 1996-09-17 | 2002-04-15 | 2233.78 | Vancouver |
2 |
| 12 | Sherlock | Holmes
| 1923-08-12 | 1945-07-21 | 5124.98 | London
|
5 |
+----+------------+-----------+------------+------------+---------+-----------+-------+
9 rows in set (0.00 sec)
mysql> create table history_job (tglUpdate date,User char(20),id_job int(7),Nama
_job char(30));
Query OK, 0 rows affected (1.87 sec)
mysql> select *from history_job;
Empty set (0.00 sec)
mysql> delimiter //
mysql> CREATE TRIGGER historyjob after UPDATE
-> ON job
-> FOR EACH ROW
-> BEGIN
-> INSERT INTO HISTORY_JOB VALUES (DATE(NOW()),user(),OLD.job_id,OLD.title
);
-> end //
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> update job set title="Forensik" where job_id=6;
Query OK, 1 row affected (1.08 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select*from history_job;
+------------+----------------+--------+-----------+
| tglUpdate | User
| id_job | Nama_job |
+------------+----------------+--------+-----------+
| 2014-02-15 | root@localhost |
6 | Forensics |
+------------+----------------+--------+-----------+
1 row in set (0.00 sec)
mysql>
mysql> update job set title="Forensics" where job_id=6;
Query OK, 1 row affected (1.79 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select*from history_job;
+------------+----------------+--------+-----------+
| tglUpdate | User
| id_job | Nama_job |
+------------+----------------+--------+-----------+
| 2014-02-15 | root@localhost |
6 | Forensics |
| 2014-02-15 | root@localhost |
6 | Forensik |
+------------+----------------+--------+-----------+
2 rows in set (0.00 sec)

mysql> create database universitas;


Query OK, 1 row affected (0.00 sec)

mysql> use universitas;


Database changed
mysql> create table instruktur(
-> NIP int(3) not null primary key,
-> namains varchar(30) not null,
-> jurusan varchar (20) not null,
-> asalkota varchar(20));
Query OK, 0 rows affected (0.83 sec)
mysql> insert into instruktur(
-> NIP, namains, jurusan, asalkota)
-> values('1', 'Steve Wozniak','Ilmu Komputer', 'Klaten'),
-> ('2', 'Steve Jobs','Seni Rupa', 'Solo'),
-> ('3', 'James Gosling','Ilmu Komputer', 'Klaten'),
-> ('4', 'Bill Gates','Ilmu Komputer', 'Magelang');
Query OK, 4 rows affected (1.79 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> create table matakuliah(
-> nomk varchar(10) not null primary key,
-> namamk varchar(50) not null,
-> sks char(3) not null);
Query OK, 0 rows affected (1.88 sec)
mysql> insert into matakuliah(
-> nomk, namamk, sks)
-> values('KOM101', 'Algoritma dan Pemrograman', '3'),
-> ('KOM102', 'Basis Data', '3'),
-> ('SR101', 'Desain Elementer', '3'),
-> ('KOM201', 'Pemrograman Berorientasi Objek', '3');
Query OK, 4 rows affected (0.03 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> create table kuliah(
-> NIP int(3) not null,
-> nomk varchar(10) not null,
-> ruangan varchar(10),
-> jmlmhs int(4)
-> foreign key (NIP) references instruktur(NIP),
-> foreign key (nomk) references matakuliah(nomk));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server vers
for the right syntax to use near 'foreign key (NIP) references instruktur(NIP),
foreign key (nomk) references mata' at line 6
mysql> create table kuliah(
-> NIP int(3) not null,
-> nomk varchar(10) not null,
-> ruangan varchar(10),
-> jmlmhs int(4),
-> foreign key (nomk) references matakuliah(nomk));
Query OK, 0 rows affected (1.86 sec)
mysql> alter table kuliah add foreign key (NIP) reference instruktur(NIP);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server vers
for the right syntax to use near 'reference instruktur(NIP)' at line 1
mysql> alter table kuliah add foreign key (NIP) references instruktur(NIP);
Query OK, 0 rows affected (1.36 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql> desc kuliah;


+---------+-------------+------+-----+---------+-------+
| Field | Type
| Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| NIP
| int(3)
| NO | MUL |
|
|
| nomk
| varchar(10) | NO | MUL |
|
|
| ruangan | varchar(10) | YES |
| NULL
|
|
| jmlmhs | int(4)
| YES |
| NULL
|
|
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> insert into kuliah(
-> NIP, nomk, ruangan, jmlmhs)
-> values('1','KOM101','101','50'),
-> ('1','KOM102','102','35'),
-> ('2','SR101','101','45'),
-> ('3','KOM201','101','55');
Query OK, 4 rows affected (0.66 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> create table log_ruang(
-> user_id varchar(15),
-> deskripsi varchar(100));
Query OK, 0 rows affected (0.06 sec)
mysql> delimiter //
mysql> \c
mysql> delimiter ;
mysql> \c
mysql> desc log_ruang;
+-----------+--------------+------+-----+---------+-------+
| Field
| Type
| Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| user_id | varchar(15) | YES |
| NULL
|
|
| deskripsi | varchar(100) | YES |
| NULL
|
|
+-----------+--------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
mysql> delimiter //
mysql> create trigger log_ruangan after update
-> ON
-> kuliah
-> for each row
-> begin
-> insert into log_ruang (user (), concat ('Merubah menjadi ', nomk, 'dari r
uang', old.ruangan,
-> 'ke ruang', new.ruangan);
-> end //
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version
for the right syntax to use near '(), concat ('Merubah menjadi ', nomk, 'dari ru
ang', old.ruangan,
'ke ruang', new' at line 6
mysql> create trigger log_ruangan after update
-> ON
-> kuliah
-> for each row
-> begin
-> insert into log_ruang (user(), concat ('Merubah menjadi ', nomk, 'dari ru
ang', old.ruangan,

-> 'ke ruang', new.ruangan);


-> end //
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version
for the right syntax to use near '(), concat ('Merubah menjadi ', nomk, 'dari ru
ang', old.ruangan,
'ke ruang', new' at line 6
mysql> create trigger log_ruangan after update
-> ON
-> kuliah
-> for each row
-> begin
-> insert into log_ruang (user (), concat ('Merubah menjadi ', nomk, 'dari r
uang', old.ruangan,
-> 'ke ruang', new.ruangan));
-> end //
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version
for the right syntax to use near '(), concat ('Merubah menjadi ', nomk, 'dari ru
ang', old.ruangan,
'ke ruang', new' at line 6
mysql> create trigger log_ruangan after update
-> ON
-> kuliah
-> for each row
-> begin
-> insert into log_ruang (user(), concat ('Merubah menjadi ', nomk, 'dari ru
ang', old.ruangan,
-> 'ke ruang', new.ruangan));
-> end //
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version
for the right syntax to use near '(), concat ('Merubah menjadi ', nomk, 'dari ru
ang', old.ruangan,
'ke ruang', new' at line 6
mysql> hgkgkh
-> ;
-> \c
mysql> create trigger log_ruangan after update
-> ON
-> kuliah
-> for each row
-> begin
-> insert into log_ruang(user(),concat ('Merubah menjadi ',kuliah.nomk, 'dar
i ruang', old.ruangan,
-> 'ke ruang', new.ruangan));
-> end //
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version
for the right syntax to use near '(),concat ('Merubah menjadi ',kuliah.nomk, 'da
ri ruang', old.ruangan,
'ke ruang'' at line 6
mysql> create trigger log_ruangan after update
-> ON
-> kuliah
-> for each row
-> begin
-> insert into log_ruang values (user(),concat ('Merubah menjadi ',kuliah.no
mk, 'dari ruang', old.ruangan,
-> 'ke ruang', new.ruangan));

-> end //
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;

Das könnte Ihnen auch gefallen