学习目标:
MySQL的基本用法
学习内容:
1.MySQL最经典的使用SQL语句来创建数据库和表;2.MySQL最基本的CRUD(增删改查);
学习时间:
.3.10
学习产出:
tips:博主用的是mysql5.7.14(跟版本没有多大关系),可视化工具是Navicat;
1、准备工作,数据表SQL
create table Websites(id int(11) primary key auto_increment,name varchar(20),url varchar(100),alexa int(11),country varchar(20))insert into Websites(name,url,alexa,country) values("Google","https://www.google.cm/",1,"USA");insert into Websites(name,url,alexa,country) values("淘宝","/",13,"CN");insert into Websites(name,url,alexa,country) values("菜鸟教程","/",4698,"CN");insert into Websites(name,url,alexa,country) values("微博","/",20,"CN");insert into Websites(name,url,alexa,country) values("Facebook","/",3,"USA");
2、select查询语句
-- 查询指定字段,(本语句查询name,url两个字段)select name url from Websites;-- 查询所有(本语句查询该表下的所有数据)select * from Websites;
3、distinct关键字
distinct关键字为过滤,即去除重复字段,加在要去重字段之前;
-- 本语句是去除country字段重复的结果select distinct country from Websites;
4、where关键字
where关键字可以做到指定条件查询,可以单字段进行指定,也可以结合and和or关键字进行指定,还可以结合算术运算符进行指定;
-- 单字段进行指定(本语句是查询country字段为CN的数据)select * from Websites where country = "CN";-- 结合and和or关键字进行指定,本语句是Alexa<15并且country为cn或者为USA的数据select * from Websites where alexa < 15 and (country = "CN" or country = "USA"); -- 结合算数运算符进行指定,本语句是Alexa<15的数据select * from Websites where alexa < 15;
5、order by关键字
order by关键字,默认asc升序,降序为desc;-- 按照Alexa升序排序select * from Websites order by alexa;-- 按照Alexa升序排列之后分组,每一组在按country进行降序排列select * from Websites order by alexa ,country desc;
6、insert into插入语句
-- 表名后可以不写具体字段,但是values后面必须按顺序写全部字段(自增字段可省略)insert into Websites values("百度","",12,"CN");-- 可以选择性插入数据,插入字段在表后写上即可,values后面按顺序进行赋值insert into Websites(name,alexa) values("百度",12);
7、update 修改语句
-- 修改菜鸟教程的Alexa值为5000,切记,如果不写where条件,将会把整张表的Alexa都修改为5000update Websites set alexa = "5000" where name = "菜鸟教程";
8、delete删除语句
-- 删除掉name值为微博的数据,切记如果不写删除条件将删除表中所有数据;delete from Websites where name = "微博";
总结:
最近朋友问我有关MySQL数据库相关的知识,我一时间也说不清楚,特写此博客,同时也算是自己复习!
此博客为系列文章,后面会有更深入的用法!
参考自:菜鸟教程SQL板块!