总结了MySQL数据表的相关操作,建了一些表的样例且给出了部分测试代码,大家可以输入到MySQL中进行验证。我会及时回复评论区的问题,如果觉得本文有帮助欢迎点赞 。
-- 测试插入记录INSERT
CREATE TABLE IF NOT EXISTS user(
id TINYINT UNSIGNED AUTO_INCREMENT KEY,
username VARCHAR(20) NOT NULL UNIQUE,
password CHAR(32) NOT NULL,
email VARCHAR(50) NOT NULL DEFAULT '[email protected]',
age TINYINT UNSIGNED DEFAULT 18
);
INSERT INTO user VALUES(1,'KING','KING','[email protected]',20);
INSERT user VALUE(2,'QUEEN','QUEEN','[email protected]',30);
INSERT user(username,password) VALUES('A','AAA');
INSERT user(password,username) VALUES('BBB','B');
INSERT user(username,id,password,email,age) VALUES('C',55,'CCC','[email protected]',DEFAULT);
-- 一次插入多条记录
INSERT user VALUES(6,'D','DDD','[email protected]',35),
(8,'E','EEE','[email protected]',9),
(18,'F','FFF','[email protected]',32);
-- 通过INSERT SET形式插入记录
INSERT INTO user SET id=98,username='test',password='this is a test',email='[email protected]',
age=48;
INSERT user SET username='maizi',password='maizixueyuan' ;
CREATE TABLE IF NOT EXISTS testUser(
id TINYINT UNSIGNED AUTO_INCREMENT KEY,
username VARCHAR(20) NOT NULL UNIQUE
);
-- 将查询结果插入到表中
INSERT testUser SELECT id,username FROM user;
-- 字段数目不匹配
INSERT testUser SELECT * FROM user;
INSERT testUser(username) SELECT username FROM user;
-- 将用户表中所有的用户年龄更新15
UPDATE user SET age=5;
UPDATE user SET age=20,email='[email protected]';
-- 将第一个记录的password,email,age
UPDATE user SET password='king123',email='[email protected]',age=99
WHERE id=1;
UPDATE user SET age=age-5 WHERE id>=3;
UPDATE user SET age=DEFAULT WHERE username='A';
-- 删除testUser表中的记录
DELETE FROM testUser ;
-- 删除user表中id为1的用户
DELETE FROM user WHERE id=1;
-- 彻底清空user表
TRUNCATE TABLE user;