insert into students values(001,张京,男,19,计算机);
insert into students values(002,李梅,女,18,数学);
建议你建一个触发器,在插入数据之前触发即可实现你要的功能。
示例代码如下:
mysql> CREATE TABLE tb(BH CHAR(16),content VARCHAR(20),`date` DATETIME,val INT);Query OK, 0 rows affected (005 sec)
mysql>
mysql>
mysql> DELIMITER $$
mysql> DROP TRIGGER IF EXISTS tri_NewBH $$
Query OK, 0 rows affected, 1 warning (000 sec)
mysql>
mysql> CREATE TRIGGER tri_NewBH BEFORE INSERT ON tb
-> FOR EACH ROW
-> BEGIN
-> DECLARE dt CHAR(8);
-> DECLARE bh_id CHAR(16);
-> DECLARE number INT;
-> DECLARE new_bh VARCHAR(16);
->
-> SET dt = DATE_FORMAT(CURDATE(),'%Y%m%d');
->
-> SELECT
-> MAX(BH) INTO bh_id
-> FROM tb
-> WHERE BH LIKE CONCAT(dt,'%');
->
-> IF bh_id = '' OR bh_id IS NULL THEN
-> SET new_bh = CONCAT(dt,'00000001');
-> ELSE
-> SET number = RIGHT(bh_id,8) + 1;
-> SET new_bh = RIGHT(CONCAT('00000000',number),8);
-> SET new_bh=CONCAT(dt,new_bh);
-> END IF;
->
-> SET NEWBH = new_bh;
-> END$$
Query OK, 0 rows affected (009 sec)
mysql>
mysql> DELIMITER ;
mysql> INSERT INTO tb(content,`date`,val) VALUES('LiangCK','2009-05-11',20);
Query OK, 1 row affected (000 sec)
mysql> INSERT INTO tb(content,`date`,val) VALUES('LiangCK','2009-05-11',20);
Query OK, 1 row affected (001 sec)
mysql> INSERT INTO tb(content,`date`,val) VALUES('LiangCK','2009-05-11',20);
Query OK, 1 row affected (000 sec)
mysql> INSERT INTO tb(content,`date`,val) VALUES('LiangCK','2009-05-11',20);
Query OK, 1 row affected (000 sec)
mysql> INSERT INTO tb(content,`date`,val) VALUES('LiangCK','2009-05-11',20);
Query OK, 1 row affected (000 sec)
mysql> INSERT INTO tb(content,`date`,val) VALUES('LiangCK','2009-05-11',20);
Query OK, 1 row affected (001 sec)
mysql> INSERT INTO tb(content,`date`,val) VALUES('LiangCK','2009-05-11',20);
Query OK, 1 row affected (000 sec)
mysql> SELECT FROM tb;
+------------------+---------+---------------------+------+
| BH | content | date | val |
+------------------+---------+---------------------+------+
| 2009051100000001 | LiangCK | 2009-05-11 00:00:00 | 20 |
| 2009051100000002 | LiangCK | 2009-05-11 00:00:00 | 20 |
| 2009051100000003 | LiangCK | 2009-05-11 00:00:00 | 20 |
| 2009051100000004 | LiangCK | 2009-05-11 00:00:00 | 20 |
| 2011051200000001 | LiangCK | 2009-05-11 00:00:00 | 20 |
| 2011051200000002 | LiangCK | 2009-05-11 00:00:00 | 20 |
| 2011051200000003 | LiangCK | 2009-05-11 00:00:00 | 20 |
+------------------+---------+---------------------+------+
7 rows in set (000 sec)
以上就是关于2、 假设本地有数据库students,数据库中有表student如下所示。编写程序向此表中添加一条记录,全部的内容,包括:2、 假设本地有数据库students,数据库中有表student如下所示。编写程序向此表中添加一条记录,、linux下mysql数据库建一个人员表关于字段的设置、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)