mysql对于【表内容】的【增删改查】
程序员文章站
2023-10-06 18:31:04
以student表为例
所展示的示例,只展示单行操作,若想实现多行操作,修改条件即可
+----+-----------------------+-----+-------...
以student表为例
所展示的示例,只展示单行操作,若想实现多行操作,修改条件即可
+----+-----------------------+-----+---------------+ | id | name | age | register_date | +----+-----------------------+-----+---------------+ | | +----+-----------------------+-----+---------------+
1.在表中插入行:
INSERT INTO table_name ( field1, field2,...fieldN ) VALUES ( value1, value2,...valueN );
示例:
insert into student (name,age,register_date) value ('zhangsan',30,'2018-02-21');
+----+------+-----+---------------+ | id | name | age | register_date | +----+------+-----+---------------+ | 1 | 张三 | 30 | 2018-02-21 | +----+------+-----+---------------+ 1 row in set (0.00 sec)
当然,只要key和value对应了,insert时候的顺序是不需要
insert into student(age,name,register_date) value (11,'wangwu','2017-03-03');
+----+--------+-----+---------------+ | id | name | age | register_date | +----+--------+-----+---------------+ | 1 | 张三 | 11 | 2018-02-21 | | 8 | lisi | 33 | 2018-03-11 | | 9 | l2isi | 33 | 2018-03-11 | | 10 | wangwu | 11 | 2017-03-03 | +----+--------+-----+---------------+ 4 rows in set (0.00 sec)
2.在表中删除行:
语法:
DELETE FROM table_name [WHERE Clause]示例:
delete from student where id = 1;
+----+--------+-----+---------------+ | id | name | age | register_date | +----+--------+-----+---------------+ | 8 | lisi | 33 | 2018-03-11 | | 9 | l2isi | 33 | 2018-03-11 | | 10 | wangwu | 11 | 2017-03-03 | +----+--------+-----+---------------+其中where最好写上,不然手一滑整张表就gg了;
3.在表中修改行
语法:
UPDATE table_name SET field1=new-value1, field2=new-value2 [WHERE Clause]示例:
update student set name = 'liugoudan',age = '3' where id = 8;
+----+-----------+-----+---------------+ | id | name | age | register_date | +----+-----------+-----+---------------+ | 8 | liugoudan | 3 | 2018-03-11 | | 9 | l2isi | 33 | 2018-03-11 | | 10 | wangwu | 11 | 2017-03-03 | +----+-----------+-----+---------------+ 3 rows in set (0.00 sec)
4.在表中查询行
语法:
SELECT field1, field2,...fieldN FROM table_name1, table_name2... [WHERE condition1 [AND [OR]] condition2.....示例:
select* from student;
+----+-----------+-----+---------------+ | id | name | age | register_date | +----+-----------+-----+---------------+ | 8 | liugoudan | 3 | 2018-03-11 | | 9 | l2isi | 33 | 2018-03-11 | | 10 | wangwu | 11 | 2017-03-03 | +----+-----------+-----+---------------+