码迷,mamicode.com
首页 > 数据库 > 详细

Mysql数据库的增删改查

时间:2018-06-07 21:50:49      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:col   安阳   add   账号   之间   .com   info   添加   lastlog   


技术分享图片

 各表中的添加的值:

技术分享图片
技术分享图片

技术分享图片
各表中的字段和属性:

技术分享图片

 

 















CREATE
DATABASE `QQlog` CREATE TABLE `QQUser` ( qqid INT PRIMARY KEY AUTO_INCREMENT NOT NULL, `password` VARCHAR(50) NOT NULL, `lastLogtime` DATETIME NOT NULL, `OnLine` INT NOT NULL, `level` INT NOT NULL ) CREATE TABLE `BaseInfo`( qqid INT PRIMARY KEY AUTO_INCREMENT NOT NULL, NickName VARCHAR(50) NOT NULL, Sex INT, Age INT NOT NULL, Privince VARCHAR(50), City VARCHAR(50), Address VARCHAR(200), Phone CHAR(50) ) CREATE TABLE `Relation`( qqid INT NOT NULL, Relationqqid INT NOT NULL, RelationStatus INT NOT NULL ) ##添加约束主外键 ALTER TABLE `QQUser` ADD CONSTRAINT fk_QQUser_Relation FOREIGN KEY (`qqid`) REFERENCES `Relation` (`qqid`); ALTER TABLE `QQUser` ADD CONSTRAINT fk_QQUser_BaseInfo FOREIGN KEY (`qqid`) REFERENCES `BaseInfo` (`qqid`); ALTER TABLE `QQUser` ADD CONSTRAINT fk_QQUser_Relation FOREIGN KEY (`Relationqqid`) REFERENCES `Relation` (`Relationqqid`); SELECT * FROM qquser SELECT * FROM BaseInfo SELECT * FROM Relation #添加数据 INSERT INTO Relation (qqid,Relationqqid,RelationStatus) VALUES (54789625,88662753,0), (88662753,88855678,1), (54789625,88855678,0) INSERT INTO BaseInfo (qqid,NickName,Sex,Age,Privince,City,Address,Phone) VALUES(88855678,独行侠,1,38,北京,海淀区,上地,003) INSERT INTO BaseInfo (qqid,NickName,Sex,Age,Privince,City,Address,Phone) VALUES (54789625,蝴蝶飞飞,1,16,北京,朝阳区,亚运村,001), (88662753,青青草,0,20,河南省,安阳,汤阴,002) INSERT INTO QQUser (qqid,`password`,lastLogtime,OnLine,`level`) VALUES (88855678,guset0221,2008-02-21 16:28:20,1,6) INSERT INTO QQUser (qqid,`password`,lastLogtime,OnLine,`level`) VALUES (54789625,add512#&,2008-02-16 17:01:35,2,1), (88662753,admin0219,2018-6-7 14:36:40,0,5) #修改时间 UPDATE QQUser SET lastLogtime =2008-02-21 16:28:20 WHERE qqid =88855678 UPDATE QQUser SET lastLogtime =2008-02-19 21:08:35 WHERE qqid =88662753 ## 01.查询QQ号码为54789625的所有好友信息,包括QQ号码,昵称,年龄 SELECT qqid,NickName,Age FROM BaseInfo WHERE(qqid = 54789625); #02.查询当前在线用户的信息,1是在线,2是离线,0是隐身 SELECT * FROM qquser,BaseInfo WHERE (BaseInfo.`qqid` = qquser.`qqid` AND OnLine = 1); #03.查询北京的、年龄在18至45岁之间的在线用户的信息 SELECT * FROM qquser,BaseInfo WHERE ( BaseInfo.`qqid` = qquser.`qqid` AND BaseInfo.`Privince` LIKE %北京% AND BaseInfo.`Age` BETWEEN 18 AND 45 AND OnLine = 1); #04.查询昵称为青青草的用户信息 SELECT * FROM BaseInfo WHERE (BaseInfo.`NickName` = 青青草); #05.查询QQ号码为54789625的用户的好友中每个省份的总人数,并且总人数按由大到小排序。 SELECT BaseInfo.`Privince`,COUNT(*) FROM Relation,BaseInfo WHERE Relation.`Relationqqid` = BaseInfo.`qqid` AND Relation.`qqid` = 54789625 GROUP BY BaseInfo.`Privince` ORDER BY COUNT(*) DESC; #06.查询至少有150天未登录QQ账号的用户信息,包括QQ号码, #最后一次登录时间、等级、 #昵称、年龄,并按时间的降序排列 SELECT BaseInfo.qqid,lastLogtime,QQUser.`level`,BaseInfo.`NickName`,BaseInfo.`Age` FROM BaseInfo,QQUser WHERE BaseInfo.`qqid` = QQUser.`qqid` AND DATEDIFF(NOW(),QQUser.`lastLogtime`)>=150 ORDER BY QQUser.`lastLogtime` DESC; #07.查询QQ号码为54789625的好友中等级为10级以上的“月亮”级用户信息。 SELECT qquser.`level`,BaseInfo.`NickName`,BaseInfo.`Privince` FROM BaseInfo,qquser,Relation WHERE BaseInfo.`qqid` = Relation.`Relationqqid` AND Relation.`qqid` = 54789625 AND Relation.`Relationqqid` = qquser.`qqid` AND qquser.`level`>=10 #08.--查询QQ号码为54789625的好友中隐身的用户信息。0是隐身 SELECT qquser.`OnLine`,BaseInfo.`NickName`,BaseInfo.`Privince` FROM BaseInfo,qquser,Relation WHERE BaseInfo.`qqid` = Relation.`Relationqqid` AND Relation.`qqid` = 54789625 AND Relation.`Relationqqid` = qquser.`qqid` AND qquser.`OnLine` = 0 #09.--查询好友超过20个的用户信息。 SELECT BaseInfo.`Privince`,BaseInfo.`NickName` FROM BaseInfo WHERE BaseInfo.`qqid` IN ( SELECT Relation.`qqid` FROM Relation WHERE Relation.`RelationStatus` = 0 HAVING COUNT(*)>20 ) #10.为了查看信誉度,管理员需要查询被当做黑名单人物次数排名前3的用户RelationStatus = 1黑名单 SELECT COUNT(*), Relation.`Relationqqid` FROM Relation WHERE Relation.`RelationStatus` = 1 GROUP BY Relation.`Relationqqid` ORDER BY COUNT(*) DESC ##用例2:修改数据 #01.假设我的QQ号码为8855678,今天我隐身登录 UPDATE qquser SET qquser.`OnLine` = 0,qquser.`lastLogtime` = NOW() WHERE qquser.`qqid` = 8855678 #02.假设我的QQ号码为8855678,修改我的昵称为“被淹死的鱼”,地址为“解放中路号院123室” UPDATE BaseInfo SET BaseInfo.`NickName` = 被淹死的鱼,BaseInfo.`Privince` = 解放中路号院123室 WHERE BaseInfo.`qqid` = 8855678 #03.假设我的QQ号码为54789625,将我的好友“青青草”拖进黑名单。 UPDATE Relation SET Relation.`RelationStatus` = 1 WHERE Relation.`qqid` = 54789625 AND Relation.`Relationqqid` = 88662753 #04.为了提高QQ用户的聊天积极性,把等级小于6级的用户的等级都提升1个级别。 UPDATE qquser SET qquser.`level` = qquser.`level`+1 WHERE qquser.`level`<6 #05.管理员将超过365天没有登录过的QQ锁定(即将等级值设定为-1)。 UPDATE qquser SET qquser.`level` = -1 WHERE DATEDIFF(NOW(),qquser.`lastLogtime`)>=365 #06.为了奖励用户,将好友数量超过20的用户等级提升1个级别。 UPDATE qquser SET qquser.`level`=qquser.`level`+1 WHERE qquser.`qqid` IN( SELECT Relation.`qqid` FROM Relation WHERE Relation.`RelationStatus`=0 GROUP BY Relation.`qqid` HAVING COUNT(Relation.`Relationqqid`)>=20) #07.把QQ号码为54789625的用户的好友“嘟嘟鱼”拖进黑名单中。 UPDATE Relation SET Relation.`RelationStatus` = 1 WHERE Relation.`qqid` = 54789625 AND Relation.`Relationqqid`= (SELECT baseinfo.`qqid` FROM baseinfo WHERE baseinfo.`NickName` = 嘟嘟鱼) AND Relation.`RelationStatus` = 0 ##用例3:删除数据 #1.把QQ号码为54789625的用户黑名单中的用户删除。 DELETE FROM Relation WHERE Relation.`qqid`=54789625 AND Relation.`RelationStatus`=1 #2.QQ号码为54789625的用户多次在QQ中发布违法信息,造成了很坏的影响,因此管理员决定将其删除。 DELETE FROM Relation WHERE Relation.`qqid` = 54789625 OR Relation.`Relationqqid` = 54789625 DELETE FROM baseinfo WHERE baseinfo.`qqid` = 54789625 DELETE FROM qquser WHERE qquser.`qqid` = 54789625 #3.管理员将超过1000天没有登录过的QQ删除。 DELETE FROM Relation WHERE Relation.`qqid` IN ( SELECT qquser.`qqid` FROM qquser WHERE DATEDIFF(NOW(),qquser.`lastLogtime`)>=1000 ) OR Relation.`Relationqqid` IN ( SELECT qquser.`qqid` FROM qquser WHERE DATEDIFF(NOW(),qquser.`lastLogtime`)>=1000 ) DELETE FROM BaseInfo WHERE BaseInfo.`qqid` IN ( SELECT qquser.`qqid` FROM qquser WHERE DATEDIFF(NOW(),qquser.`lastLogtime`)>=1000 ) DELETE FROM qquser WHERE DATEDIFF(NOW(),qquser.`lastLogtime`)>=1000

 

Mysql数据库的增删改查

标签:col   安阳   add   账号   之间   .com   info   添加   lastlog   

原文地址:https://www.cnblogs.com/www-yang-com/p/9152721.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!