1.建表与删表
建表:
create table 表名(
字段名 类型 约束(主键,非空,唯一,默认值)
字段名 类型 约束(主键,非空,唯一,默认值)
) 编码,存储引擎;
删表:drop table if exists 表名
在SQL中,有如下约束:
NOT NULL - 指示某列不能存储 NULL 值。
UNIQUE - 保证某列的每行必须有唯一的值。
PRIMARY KEY - NOT NULL 和 UNIQUE 的结合。确保某列(或两个列多个列的结合)有唯一标识,有助 于更容易更快速地找到表中的一个特定的记录。
FOREIGN KEY - 保证一个表中的数据匹配另一个表中的值的参照完整性。
CHECK - 保证列中的值符合指定的条件。 DEFAULT - 规定没有给列赋值时的默认值。
如下实例
DROP TABLE IF EXISTS `websites`;
CREATE TABLE `websites` (
id int(11) NOT NULL AUTO_INCREMENT,
name char(20) NOT NULL DEFAULT '' COMMENT '站点名称',
url varchar(255) NOT NULL DEFAULT '',
alexa int(11) NOT NULL DEFAULT '0' COMMENT 'Alexa 排名',
sal double COMMENT '广告收入',
country char(10) NOT NULL DEFAULT '' COMMENT '国家',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2.插入,删除与更新
插入语句:insert into 表名()values();
删除语句:delete from 表名 where 条件;
更新语句:update 表名 set 更新的操作 where 条件
如下图实例
插入语句:
INSERT INTO websites(name, url,alexa,sal,country ) VALUES ('腾讯', 'https://www.qq.com',
18, 1000,'CN' ) ;
删除语句:
delete from websites where id = 5;
更新语句:
update websites set sal = null where id = 3
3.select查询语句
先如下图建表,并初始化数据
DROP TABLE IF EXISTS `websites`;
CREATE TABLE `websites` (
id int(11) NOT NULL AUTO_INCREMENT,
name char(20) NOT NULL DEFAULT '' COMMENT '站点名称',
url varchar(255) NOT NULL DEFAULT '',
alexa int(11) NOT NULL DEFAULT '0' COMMENT 'Alexa 排名',
sal double COMMENT '广告收入',
country char(10) NOT NULL DEFAULT '' COMMENT '国家',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `websites` VALUES
(1, 'Google', 'https://www.google.cm/', '1', 2000,'USA'),
(2, '淘宝', 'https://www.taobao.com/', '13',2050, 'CN'),
(3, '菜鸟教程', 'http://www.runoob.com/', '4689',0.0001, 'CN'),
(4, '微博', 'http://weibo.com/', '20',50, 'CN'),
(5, 'Facebook', 'https://www.facebook.com/', '3', 500,'USA');
CREATE TABLE IF NOT EXISTS `access_log` (
`aid` int(11) NOT NULL AUTO_INCREMENT,
`site_id` int(11) NOT NULL DEFAULT '0' COMMENT '网站id',
`count` int(11) NOT NULL DEFAULT '0' COMMENT '访问次数',
`date` date NOT NULL,
PRIMARY KEY (`aid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `access_log` (`aid`, `site_id`, `count`, `date`) VALUES
(1, 1, 45, '2016-05-10'),
(2, 3, 100, '2016-05-13'),
(3, 1, 230, '2016-05-14'),
(4, 2, 10, '2016-05-14'),
(5, 5, 205, '2016-05-14'),
(6, 4, 13, '2016-05-15'),
(7, 3, 220, '2016-05-15'),
(8, 5, 545, '2016-05-16'),
(9, 3, 201, '2016-05-17'),
(10, 88, 9999, '2016-09-09')
查询语句:
select * from 表名
select 列名 from 表名(推荐使用)
如下图实例
select * from websites
select id, name, url, alexa, sal, country from websites (推荐使用的方式)
即可出现如下结果
4.分页查询
如下代码
select * from websites limit 2,3 ; --从第2条(下标从0开始)开始查,查3条数据
select * from websites limit 3 ; --从第0条(下标从0开始)开始查,查3条数据
注:以上两个代码均会出现三个结果,区别在于初始位置不同
如下图所示用法:
5.distinct关键字
distinct 关键词用于返回唯一不同的值
如下代码
select distinct country from websites
如下图所示用法
由此可见,distinct可将相同的元素省略,只显示不同的元素
6.where语句
作为条件筛选, 运算符: > < >= != =
is null is not null (因为在sql 语句中null 和任何东西比较都是假,包括它本身)
like in
如下代码
select * from websites where sal >500
所示代码会将sal值大于500的显示出来
注:若想筛选出为空值的数据,要用is null或is not null,不能使用=null或!=null
7.逻辑条件
如下代码
select * from websites where sal >= 0 and sal <=2000 ; -- 收入在 0 到 2000 之间
select * from websites where sal between 0 and 2000; -- 和上面一样的,没事找事
select * from websites where sal < 5 or sal is null ; -- 收入小于5 或者没收入
同理,只能使用is null或is not null
8.order by
排序: 默认情况下是升序,asc 可以省略
如下代码
select * from websites order by sal asc;--根据sal 升序排序
select * from websites order by alexa desc;--根据 alexa 降序排序
简单的排序语句,不多赘述
9.like和通配符
like 模糊查询
通配符
% : 0个或多个字符
_ : 1 个字符
如下代码
select * from websites where name like '_o%';
select * from websites where name like '%o%';
第一个代码会显示出o前只有一个字母,o后没有字母或有多个字母的所有数据
第二个代码会显示出o前没有字母或有多个字母,且o后也没有字母或有多个字母的所有数据
10.in
同样为条件筛选,区别在于in可以更简单地匹配多个条件
如下代码
select * from websites where country in ('USA','CN');
等价于
select * from websites where country = 'USA' or country = 'CN
11.别名
没有什么难点,给表取个别名,方便使用
如下代码,将给websites取一个名为tt的别名
select tt.name '网站名字' from websites tt;
12.Group by 分组查询
将数据分组,一般用于求平均数等情况
注意:分组时候的筛选用 having
常见的几个组函数: max() min() avg() count() sum()
如下代码
select avg(sal) aa from websites where sal is not null group by country having aa > 1500
此代码会将websites中sal不为空的数据按照country分组,并且将每一组sal的平均值求出来,将此平均值的别名定为aa,最后输出aa大于1500的数据
13.子查询
把查询的结果当作一个表来使用
14.连接查询
如下代码
select name,count,date from websites w , access_log a ; --著名的笛卡尔积,没什么意义的
select name,count,date from websites w , access_log a where w.id = a.site_id;-- 这是 1992
的语法
select name,count,date from websites w inner join access_log a on w.id = a.site_id;-- 这是
1999 年的语法,推荐使用
select name,count,date from websites w left outer join access_log a on w.id = a.site_id;--
把没有访问的网站也显示出来
-- 注意: inner 和 outer 是可以默认省略的
on后为关联条件,where后为筛选条件
left是以左表为主,right是以右表为主,union可将余下数据也输出出来
15.Null 处理l 函数
如下代码
select name,ifnull(count,0),ifnull(date,'') from websites w left outer join access_log a on
w.id = a.site_id
若count为null,将用0代替null;若date为null,将用空格代替null