· 8 years ago · Feb 18, 2018, 05:50 PM
1mysql> DROP TABLE IF EXISTS table1, table2;
2
3Query OK, 0 rows affected (0.01 sec)
4
5mysql> CREATE TABLE IF NOT EXISTS table1 (
6 -> id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
7 -> col_1 VARCHAR(45) NOT NULL ,
8 -> col_2 VARCHAR(45) NOT NULL ,
9 -> PRIMARY KEY (`id`))
10 -> ENGINE = InnoDB;
11
12Query OK, 0 rows affected (0.01 sec)
13
14mysql> CREATE TABLE table2 LIKE table1;
15Query OK, 0 rows affected (0.00 sec)
16
17mysql> INSERT INTO table1 (id, col_1, col_2) VALUES (NULL, "xxx","yyy");
18Query OK, 1 row affected (0.00 sec)
19
20mysql> INSERT INTO table2 (id, col_1, col_2) SELECT NULL, NULL, col_2 FROM table1 WHERE id=1;
21Query OK, 1 row affected, 1 warning (0.00 sec)
22Records: 1 Duplicates: 0 Warnings: 1
23
24mysql> SHOW WARNINGS;
25+---------+------+-------------------------------+
26| Level | Code | Message |
27+---------+------+-------------------------------+
28| Warning | 1048 | Column 'col_1' cannot be null |
29+---------+------+-------------------------------+
301 row in set (0.00 sec)
31
32mysql> SELECT * FROM table2;
33+----+-------+-------+
34| id | col_1 | col_2 |
35+----+-------+-------+
36| 1 | | yyy |
37+----+-------+-------+
381 row in set (0.00 sec)
39
40mysql> INSERT INTO table2 (id, col_1, col_2) VALUES( NULL, NULL, "zzz");
41ERROR 1048 (23000): Column 'col_1' cannot be null
42
43mysql> SELECT * FROM table2;
44+----+-------+-------+
45| id | col_1 | col_2 |
46+----+-------+-------+
47| 1 | | yyy |
48+----+-------+-------+
491 row in set (0.00 sec)
50
51CREATE TRIGGER triggerName BEFORE INSERT ON customer
52FOR EACH ROW
53BEGIN
54 if new.`customerName` = '' then
55 signal sqlstate '45000'
56 SET MESSAGE_TEXT = 'Customer Name Cannot be Empty!';
57 end if;
58END