JavaWeb笔记001 sql基本语法

本文是个人自学过程中总结的SQL的基本使用,主要是基本的增删改查,适合初学者。

sql的分类:

	DDL:数据定义语言
		操作对象:数据库和表
		关键词:create alter drop
	DML:数据操作语言
		操作对象:记录
	DQL:数据查询语言(非官方)
	DCL:数据控制语言
		操作对象:用户 事务 权限

登录数据库:

	mysql -uroot  -p密码

DDL:数据定义语言

操作对象:数据库和表

关键词:create alter drop

操作数据库:

创建:
	格式:
		create database 数据库名称;
		-- 创建数据库时,设置数据库的编码方式 
        -- CHARACTER SET:指定数据库采用的字符集,utf8不能写成utf-8
        -- COLLATE:指定数据库字符集的排序规则,utf8的默认排序规则为utf8_general_ci(通过show character set查看)
        create database dbtest CHARACTER SET utf8 COLLATE utf8_general_ci;
删除:
    格式:
         drop database 数据库名称;
修改数据库编码:
    -- 修改数据库编码
    alter database dbtest CHARACTER SET GBK COLLATE gbk_chinese_ci;
    alter database dbtest CHARACTER SET utf8 COLLATE utf8_general_ci;
常用的命令:
	查看所有的数据库: show databases;

操作表:

1.创建表
格式:
	create table 表名(字段描述,字段描述);
	字段描述:
		字段名称 字段类型 [约束]
	例如:
		create table user(
			id int primary key auto_increment,
			username varchar(20)
		);
		
		create table user1(
			id int primary key auto_increment,
			username varchar(20)
		);
        -- 创建表时,设置表、字段编码
            use dbtest;
            drop table if exists tbtest;
            create table tbtest(
            id int(10) auto_increment,
            user_name varchar(60) CHARACTER SET GBK COLLATE gbk_chinese_ci,
            email varchar(60),
            PRIMARY key(id)
            )CHARACTER SET utf8 COLLATE utf8_general_ci;
        -- 修改表编码
            alter table tbtest character set utf8 COLLATE utf8_general_ci;
2.修改表
格式:
	alter table 表名 ....
修改表名:
	alter table 旧表名 rename to 新表名;
	例如:
		alter table user1 rename to user10;
添加字段:
	alter table 表名 add [column] 字段描述;
	例如:
		alter table user add password varchar(20);
修改字段名:
	alter table 表名 change 字段名称 新字段描述;
	例如:
		alter table user change password pwd varchar(20);
修改字段描述:
	alter table 表名 modify 字段名称 字段类型 [约束];
	例如:
		alter table user modify pwd int;
删除字段:
	alter table 表名 drop 字段名;
	例如:
		alter table user drop pwd;
修改字段编码:
        ALTER TABLE tbtest MODIFY email VARCHAR(60) CHARACTER SET utf8 COLLATE utf8_general_ci;
3.删除表:
格式:
	drop table 表名;
		
常用命令:
	切换或者进入数据库: use 数据库名称;
	查看当前数据库下所有表: show tables;
	查看表结构:desc 表名;
	查看建表语句:show create table 表名;
	查看所有的字符编码:SHOW CHARACTER SET;
	查看数据库编码:show variables like '%char%';

DML:数据操作语言

操作对象:记录(行)

	关键词:insert update delete
1 插入:
格式1:
	insert into 表名 values(字段值1,字段值2...,字段值n);
	注意:
		默认插入全部字段,
		必须保证values后面的内容的类型和顺序和表结构中的一致
		若字段类型为数字,可以省略引号
	例如:
		insert into user values(1,'tom');
		insert into user values('2','tom');
		insert into user values('3');-- 错误的  

格式2:
	insert into 表名(字段名,字段名1...) values(字段值,字段值1...);
	注意:
		插入指定的字段
		必须保证values后面的内容的类型和顺序和表名后面的字段的类型和顺序保持一致.
	例如:
		insert into user (username,id) values('jack',4);
		insert into user (username) values('jack',5);-- 错误的
2 修改:
			
格式:
	update 表名 set 字段名=字段值,字段名1=字段值1... [where 条件];
例如:
	update user set username='jerry' where username='jack';
3 删除:
格式:
	delete from 表名 [where 条件];
例如:
	delete from user where id = '2';


DQL:数据查询语言

		关键词:select
格式:
	select ... from 表名 where 条件 group by 分组字段 having 条件 order by 排序字段 ase|desc

初始化环境:
	-- 创建商品表
	create table products(
		pid int primary key auto_increment,
		pname varchar(20),
		price double,
		pnum int,
		cno int,
		pdate timestamp
	);

	insert into products values (null,'泰国大榴莲',98,12,1,null);
	insert into products values (null,'新疆大枣',38,123,1,null);
	insert into products values (null,'新疆切糕',68,50,2,null);
	insert into products values (null,'十三香',10,200,3,null);
	insert into products values (null,'老干妈',20,180,3,null);
	insert into products values (null,'豌豆黄',20,120,2,null);

练习:

简单查询:
练习:
1.查询所有的商品
	select * from products;
2.查询商品名和商品价格.
	-- 查看指定的字段 
	-- 格式: select 字段名1,字段名2 from 表名
	select pname,price from products;
3.查询所有商品都有那些价格.
	-- 去重操作 distinct
	-- 格式: select distinct 字段名,字段名2 from 表名
	select price from products;
	select distinct price from products;
4.将所有商品的价格+10元进行显示.(别名)
	-- 可以在查询的结果之上进行运算,不影响数据库中的值
	-- 给列起别名 格式: 字段名 [as] 别名
	select price+10 from products;
	select price+10 新价格 from products;
	select price+10 '新价格' from products;
	select price+10 新 价 格 from products;-- 错误
	select price+10 '新 价 格' from products;
	select price+10 `新 价 格` from products;


条件查询:
练习:
1.查询商品名称为十三香的商品所有信息:
	select * from products where pname = '十三香';
2.查询商品价格>60元的所有的商品信息:
	select * from products where price>60;
3.查询商品名称中包含”新”的商品
	-- 模糊匹配 
	--	格式: 字段名 like "匹配规则";
	--		匹配内容 %
					"龙"	值为龙
					"%龙"	值以"龙"结尾
					"龙%"	值以"龙"开头
					"%龙%" 	值包含"龙"
	--		匹配个数 "__" 占两个位置
	
	select * from products where pname like "%新%";
4.查询价格为38,68,98的商品
	select * from products where price = 38 or price = 68 or price=98;
	select * from products where price in(38,68,98);
	
	where后的条件写法:
		* > ,<,=,>=,<=,<>,!=
		* like 使用占位符 _ 和 %  _代表一个字符 %代表任意个字符. 
			* select * from product where pname like '%新%';
		* in在某个范围中获得值.
			* select * from product where pid in (2,5,8);
		* between 较小值 and 较大值
			select * from products where price between 50 and 70;

排序查询:
1.查询所有的商品,按价格进行排序.(asc-升序,desc-降序)
	select * from products order by price desc;
2.查询名称有新的商品的信息并且按价格降序排序.
	select * from products where pname like '%新%' order by price  desc;


聚合函数:
	对一列进行计算 返回值是一个,忽略null值
* sum(),avg(),max(),min(),count();
1.获得所有商品的价格的总和:
	select sum(price) from products;
2.获得商品表中价格的平均数:
	select avg(price) from products;
	-- round(值,保留小数位)
	select round(avg(price),2) from products;
3.获得商品表中有多少条记录:
	select count(*) from products;

分组:使用group by
1.根据cno字段分组,分组后统计商品的个数,其实就是总条数.
	select cno,count(*) from products group by cno;
2.根据cno分组,分组统计每组商品的总数量,并且总数量> 200;
	select cno,sum(pnum) from products  group by cno;
	select cno,sum(pnum) from products  group by cno having sum(pnum)>200;
	注意:
		where和having区别:
			1.where 是对分组前的数据进行过滤 ;having 是对分组后的数据进行过滤 
			2.where 后面不能使用聚合函数,having可以

数据类型:

java			mysql		
byte			tinyint			
short			smallint
int				int(★)
long			bigint
char/String		varchar(★)|char
					varchar:可变长度 mysql的方言  varchar(20):  存放abc 只会占用三个
					char:固定长度 char(20) 存放abc 占用20个
boolean			tinyint|int 代替
float|double	float|double
					注意:
						double(5,2):该小数长度为5个,小数占2个  最大值:999.99


java.sql.Date		date 日期
java.sql.Time		time 时间
java.sql.Timestamp	timestamp(★) 时间戳 若给定值为null,数据库会把当前的系统时间存放到数据库中
					datetime(★) 日期+时间
					
java.sql.Clob(长文本)	mysql的方言(text)
java.sql.Blob(二进制)	blob

约束

作用:
	为了保证数据的有效性和完整性
mysql中常用的约束:主键约束(primary key)  唯一约束(unique) 非空约束(not null) 外键约束(foreign key)

主键约束:被修饰过的字段唯一非空

注意:一张表只能有一个主键,这个主键可以包含多个字段
方式1:建表的同时添加约束 格式: 字段名称 字段类型 primary key
方式2:建表的同时在约束区域添加约束 
	所有的字段声明完成之后,就是约束区域了
	格式: primary key(字段1,字段2)
	
	create table pk01(
		id int,
		username varchar(20),
		primary key (id)
	);
	
	insert into pk01 values(1,'tom');-- 成功
	insert into pk01 values(1,'tom');-- 失败 Duplicate entry '1' for key 'PRIMARY'
	insert into pk01 values(null,'tom');-- 失败  Column 'id' cannot be null
	
	create table pk01(
		id int primary key,
		username varchar(20),
		primary key (id)
	);-- 错误的 一张表只能有一个主键
	
方式3:建表之后,通过修改表结构添加约束
	create table pk02(
		id int,
		username varchar(20)
	);
	
	alter table pk02 add primary key(字段名1,字段名2..);
	alter table pk02 add primary key(id,username);
	
	insert into pk02 values(1,'tom');-- 成功
	insert into pk02 values(1,'tomcat');-- 成功
	insert into pk02 values(1,'tomcat');-- 失败

唯一约束:

被修饰过的字段唯一,对null不起作用
方式1:建表的同时添加约束 格式: 字段名称 字段类型 unique
	create table un(
		id int unique,
		username varchar(20) unique
	);
	
	insert into un value(10,'tom');-- 成功
	insert into un value(10,'jack');-- 错误 Duplicate entry '10' for key 'id'
	insert into un value(null,'jack');-- 成功
	insert into un value(null,'rose');-- 成功
	
方式2:建表的同时在约束区域添加约束 
	所有的字段声明完成之后,就是约束区域了
	unique(字段1,字段值2...)
方式3:建表之后,通过修改表结构添加约束
	alter table 表名 add unique(字段1,字段2);-- 添加的联合唯一
	alter table 表名 add unique(字段1);-- 给一个添加唯一
	alter table 表名 add unique(字段2);-- 给另一个添加唯一
	
		create table un01(
			id int,
			username varchar(20)
		); 
		alter table un01 add unique(id,username);
		insert into un01 values(1,'tom');-- 成功
		insert into un01 values(1,'jack');-- 成功
		insert into un01 values(1,'tom');-- 失败  Duplicate entry '1-tom' for key 'id'

非空约束

create table nn(
	id int not null,
	username varchar(20) not null
);

insert into nn values(null,'tom');--  错误的 Column 'id' cannot be null


limit

limit可以提高效率,例如按照id查找用户,确定只能查到一条,可以在最后加上limit 1,查到之后就不在查找了

case when

例如sex字段

CASE sex
WHEN '1' THEN '男'
WHEN '2' THEN '女'
ELSE '其他' END

ASE WHEN sex = '1' THEN '男' 
WHEN sex = '2' THEN '女' 
ELSE '其他' END  

IFNULL

IFNULL(expr1,expr2)的用法:

假如expr1 不为 NULL,则 IFNULL() 的返回值为 expr1; 
否则其返回值为expr2。IFNULL()的返回值是数字或是字符串

例子

select (case when  t1.main_mid = t13.mid then t13.id else t4.id end) as id,(case when t1.main_mid = t13.mid then t1.mid else t4.mid end ) as mid,t4.adxid,t4.tanxid,t4.saxid,t4.tadeid,t4.qaxid, t20.value as title,t21.value as description,t22.value as subimage,t23.value as oppoformatspec, 
 t4.adxadcategory,t4.tanxadcategory,t4.baiduadcategory,t4.tadeadcategory,t4.qaxadcategory,t4.amaxadcategory, 
 (case when  t1.main_mid = t13.mid then t13.duration else t4.duration end) as duration, (case when  t1.main_mid = t13.mid then t13.clicktype else t4.clicktype end) as clicktype, 
 t4.gdtadcategory,t4.youkuadcategory,t4.youkuid, t1.clickthrough, t1.clickurl, t1.autopause, 
 (case when  t1.main_mid = t12.mid then t12.`value` else t8.`value` end) as CHANNEL_MID,(case when t1.main_mid = t15.mid then t15.`value` else t9.value end ) as CHANNEL_ADCATEGORY 
 from Adx.Ad3 t1 
 left join Adx.Plan t2 on t1.planid=t2.planid 
 left join Adx.AdxCertStatus t3 on t1.cid = t3.cid 
 left join Adx.Ad3ExtSetting t4 on t1.mid=t4.mid 
 left join Adx.Ad3KVSetting t5 on t1.mid = t5.mid and t5.name = 'OPPO_STATUS' 
 left join Adx.CustomerKVSetting t7 on t1.cid = t7.cid and t7.name = 'OPPO_CHANNELENABLE' 
 left join Adx.Ad3KVSetting t8 on t1.mid = t8.mid and t8.name = 'OPPO_MID' 
 left join Adx.Ad3KVSetting t9 on t1.mid = t9.mid and t9.name = 'OPPO_ADCATEGORY' 
 left join Adx.Ad3KVSetting t10 on t1.main_mid = t10.mid and t10.name = 'OPPO_STATUS' 
 left join Adx.Ad3KVSetting t12 on t1.main_mid = t12.mid and t12.name = 'OPPO_MID' 
 left join Adx.Ad3KVSetting t15 on t1.main_mid = t15.mid and t15.name = 'OPPO_ADCATEGORY' 
 left join Adx.Ad3ExtSetting t13  on t1.main_mid=t13.mid 
 left join Adx.Ad3KVSetting t20 on t1.mid = t20.mid and t20.name = 'TITLE' 
 left join Adx.Ad3KVSetting t21 on t1.mid = t21.mid and t21.name = 'DESCRIPTION' 
 left join Adx.Ad3KVSetting t22 on t1.mid = t22.mid and t22.name = 'SUBIMAGE' 
 left join Adx.Ad3KVSetting t23 on t1.mid = t23.mid and t23.name = 'OPPO_FORMAT_SPEC' 
 left join Adx.Ad2 t14  on t1.src2=t14.src2 
 left join Adx.Ad3KVSetting t11 on t1.mid = t11.mid and t11.name = 'OPPO_BIDENABLE'
 left join Adx.Ad3KVSetting t16 on t1.main_mid = t16.mid and t16.name = 'OPPO_BIDENABLE'
 where t3.autopause=0 and t1.autopause in (0,1) and t1.pause=0 and t2.pause=0 and t2.autopause=0 
 and now()>=IFNULL(t2.startdate,now()) and now()<=DATE_ADD(IFNULL(t2.enddate, now()), INTERVAL 1 DAY) 
 and (t5.value = '0' OR t10.`value` = '0')
  and t1.syncflag=1 and t1.agauditstatus=0 
 and (t11.value = '1' or t16.`value` = '1')
 and t7.value = '1' 
 and t14.pause=0 ;  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章