schooldb库——utf8字符集——utf8_general_ci排序规则
先创建库,再去使用下列的DDL语句。
DDL
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '学号',
`createDate` datetime DEFAULT NULL,
`userName` varchar(20) DEFAULT NULL,
`pwd` varchar(36) DEFAULT NULL,
`phone` varchar(11) DEFAULT NULL,
`age` tinyint(3) unsigned DEFAULT NULL,
`sex` char(2) DEFAULT '男',
`introduce` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `result` (
`rid` int(11) NOT NULL AUTO_INCREMENT COMMENT '成绩编号',
`testName` varchar(255) DEFAULT NULL COMMENT '测试名称',
`score` double(4,2) DEFAULT NULL COMMENT '成绩',
`studentId` int(11) DEFAULT NULL COMMENT '学生id',
PRIMARY KEY (`rid`),
KEY `studentId` (`studentId`),
DML
CONSTRAINT `result_ibfk_1` FOREIGN KEY (`studentId`) REFERENCES `student` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
insert into student values(0,'2024-02-25 10:10:10','赵灵儿','123',
'15612345678',16,'女','逍遥哥哥,你终于找到我了。');
insert into student values(0,'2024-02-25 10:10:10','王语嫣','123',
'15612345678',17,'女','慕容复,我和你不共戴天。');
insert into student values(0,'2024-02-25 10:10:10','龙姑娘','123',
'15612345678',22,'女','我想过过过儿过过的日子。');
insert into student values(0,'2024-02-25 10:10:10','杨过','123',
'15612345678',18,'男','一遇杨过误终身。');
insert into student values(0,'2024-02-25 10:10:10','杨逍','123',
'15612345678',27,'男','杨过跟程英的大儿子。');
insert into student (userName,age,introduce)values('黄衣女子',26,'杨过与龙姑娘的大女儿。');
insert into result values(0,'政治',96,1);
insert into result values(0,'地理',86,5);
insert into result values(0,'政治',77,3);
insert into result values(0,'英语',99,3);
insert into result values(0,'历史',22,6);
insert into result values(0,'化学',69,2);
insert into result values(0,'Java',79,5);
insert into result values(0,'Java',67,6);
insert into result values(0,'政治',97,2);
insert into result values(0,'物理',76,6);
insert into result values(0,'物理',82,2);
insert into result values(0,'化学',56,4);
DQL
#变量的定义方法
#set#我们在update语句在使用过set,update声明修改表,set声明修改列
set@userName='建筑';
set@userName:='阿道夫';
#查询中赋值
SELECT @userName;
SELECT @userName as '名称';
#一句话定义多个变量,使用【,】区分
set @userName1='达到',@userName2='喝喝咖啡';
SELECT @userName1,@userName2;
#MySQL变量的四则运算以及取模操作
set @x=5,@y=0,@dx=0.1,@dy=5.65;
SELECT @x+@y;
SELECT @x-@y;
SELECT @x*@y;
SELECT @x/@y;
SELECT @x%@y;
#在浮点数运算时返回的结果会补充很多0,需要消除这些无用的0
SEt @result1 = @dx + @dy;
SEt @result2 = @dx - @dy;
SEt @result3 = @dx * @dy;
SEt @result4 = @dx / @dy;
SEt @result5 = @dx % @dy;
SELECT @result1,@result2,@result3,@result4,@result5;
# 关系运算符与逻辑运算符
set @x=5 ,@y=7;
SELECT @x<=@y;
SELECT@x>@y;
#逻辑运算符
select true and true;
SELECT FALSE or true;
#变量在SQL当中的使用
set @userName='龙姑娘';
set @age=16;
SELECT * FROM student WHERE userName=@userName or age=@age;
#需要进行多个查询的时候不能使用字符串拼接,因为无法识别,或关键字
set @userName1='龙姑娘';
set @userName2='赵灵儿';
SELECT * FROM student where userName in (@userName1,@userName2);