今天学习了数据库SQL语句的一些基本操作,从创建数据库开始,
创建一个数据库名字叫bd1;
create database db1;
创建一个数据库并指定编码gbk:
create database db1 character set gbk;
查询数据库:
show databases;
查看定义数据库的信息:
show create database db1;
删除数据库:
drop database db1;
ps:删库之前需要备份(谨慎)库一旦没有了 里面所有的表都没有 表都没有数据都没
修改操作:
修改数据库的编码集:
alter database db1 character set utf8;
其他: 1.使用使用一个数据库: use 数据库名字; 2.查询当前使用的数据库: select database();
表的操作:
创建表:
create table 表名(
列表1 列的类型 [约束],
列表2 列的类型 [约束],
列表3 列的类型 [约束],
.....
列表n 列的类型 [约束],
);
ps:创建表的时候建议换行书写(为啥呢,因为好看 :) )
注意
如果创建表的时候使用了关键字
例如创建一张订单表(order), order是一个数据库中的关键字(排序) ,此时最好使用 t_order来代替, 和关键字区分开( t 代表着table), 若果列中出现order, 则 用 ’ order ’ 代替
关于char和varchar的选择
字符型的有char 和 varchar
他们的区别是char是定长, 而varchar是可变的
char的取值范围0-255
varchar 的取值范围 0- 65535
例如::
char(5) –> 可以存储5个字符并且在内存空间存5个字节
例如: char类型的
‘1’ –> 5个字节
‘11’–> 5个字节
他们的大小是不可变的
varchar(5)–> 可以存储5个字符并且在内存空间存实际存储字符个数的字节
例如: ‘1’ –> 1个字节
‘11’ –> 2个字节
可变的
无论是varchar还是char只要超过存储范围,都会被截取并且提示警告
一般情况下要存储字符串varchar
查询当前数据库中的表
show tables;
查看表中的信息
desc 表名;
在表中添加一条新的列
alter table 表名 add 列名 数据类型;
删除表中的一列
alter table 表名 drop 列名;
更改表的名称
rename table 原表名 to 新表名;
查看表的细节(建表的信息)
show create table 表名;
修改表的字符集
alter table 表名 character set 字符集;
修改列名
alter table 表名 change 原有列名 新列名 数据类型
删除表
drop table 表名;
表中的约束:(只能针对列进行约束)
1.非空约束 NOT NULL 不许当前类为空的内容
2.设置默认值 DEFAULT
3.唯一约束 在当前列中的内容必须是唯一的 unique
4.主键 PRIMARY KEY 非空且唯一
5.主键自动增长 AUTO_INCREM
插入操作
语法:
insert into 表名(列1,列2...) values(对应的值1,值2...);
ps:1.列名和列值要一一对象, 可以将插入表中的数据,看做java中方法形参的赋值
2.值不要超过定义的长度
3.如果插入的是空值,使用null
4.插入的日期和字符串一定要使用”引起来
修改操作
语法:
update 表名 set 列名 = 列值...; -->修改所有表中对于列的值
删除操作
语法:
delete from 表名; -->删除表(表中的数据)
delete from 表名 where 列名=值 -->删除一套记录;
下面就到了SQL语句中最重要地方了!! —->DQL操作(查询语句)
数据查询操作,DQL的操作语句不会对数据进行改变,而是将数据结果展示给用户
以product数据库为栗子进行操作, 表的内容如下
先写一点知识点,后面写栗子
设置列名的别名 –> AS
1.改变猎德标题头
2.用于表示计算的结果
3.作为列的别名存在
4.如果别名中使用了特殊字符,或者强制大小写,或空格,都需要使用”引来
设置显示格式查询:
为了使用户浏览数据更加方便们可以使用一个关键字 ‘CONCAT函数’来连接字符串
查询商品的名字和零售价格 格式:XXX商品的零售价格为:XXX
select CONCAT(productName,’商品的零售价格为:’,salePrice) as productSalePrice from product;
过滤查询:
语法: SELECT 列 FROM 表 WHERE 子句限定(查询限定条件)
ps:where是一个条件限定语句where后面会跟随着你所要限定的条件
SQL中查询语句中的执行顺序
1.from子句 确定是从哪一张表中取做的查询
2.where子句:从表中直接筛选出符合条件数据
3.select子句:从筛选之后的记过中集中显示出某些类
4.ORDER BY子句:对查询结果排序
逻辑运算符:
AND(&&) 组合的条件都为true时返回值true
OR(||) 组合的条件只要有一个为true返回true
NOT(!) 若条件是false 返回true
这里注意了,要写and和or,千万别写成了 && 和 || , 今天做题我就是写成了&&以为自己没错,结果找了好久都不知道错哪里
范围查询 BETWEEN AND
使用BERWEEN运算符显示某一值的范围记录,这个操作符常见的使用在数字类型或日期类型的范围上
语法:
SELECT 列 FROM 表 WHERE 列 BETWEEN 最小值 AND 最大值;
使用IN运算符
判断类的值时是否存在指定的集合中
语法:
SELECT 列 FROM 表 WHERE 列 IN(值1,值2….)
空值查询- IS NULL
NULL表示某一列没有数据也不是空字符串
IS NULL 判断列的值是是否为NULL
模糊查询 like
使用like运算符执行通配查询,查询条件可以包含文字字符或数字
通配符匹配模式:
% : 可表示零或多个任意字符
_ : 可以表示一个字符
结果排序
使用ORDER BY 子句将查询的记录进行排序
ASC : 升序 可以省略
DESC : 降序
ps: ORDER BY 语句出现在SELECT语句的最后
ps:select语句执行循序
先执行from –>然后Where–>然后selec–>最后order by
1.首先是最简单的查看所有的数据, *表示全部
select *from product;
2.将product表中的商品id,商品名称,零售价格查询出来
select id,product,salePrice from product;
3.需求查询商品分类编号
select dir_id from product;
4.查询商品编号是不是有重复
select distinct dir_id from product;
5.查询所有货品Id,名称和批发价格(批发价格=零售 * 折扣)
select id,productName, salePrice*cutoff from product;
6.查询所有货品id,名称和个进50个的成本价格(成本=costPrice)
select id,productName, costPrice*50 from product;
7.查询所有货品id,名称和个进50个的成本价格(成本=costPrice),并且每个运费成本是1元
select id,productName,(costPrice+1)*50 from product;
8.查询商品的名字和零售价格 格式:XXX商品的零售价格为:XXX
select concat(productName ,'商品的零售价格为:',salePrice) from product;
9.查询货品零售价格为119的所有货品信息
select * from product where salePrice = 119;
10.查询获评名称为'罗技G9X'的所有货品信息
select *from product where productName ='罗技G9X';
11.需求:查询货品名称不为'罗技G9X'的所有货品信息
select *from product where productName !='罗技G9X';
12.查询分类编号不等于2的货品信息
select dir_id from product where dir_id != 2;
13.查询货品名称,零售价格小于等于200的货品
selectd productName ,salePrice from product where salePrice <= 200;
14.查询id,货品名称,批发价格(零售*折扣)大于350的货品
select id, productName, salePrice*cutoff from product where salePrice*cutoff >350;
14.这个列名salePrice * cutoff不好修改 pf
select id,productName,salePrice*cutoff as pf from product where salePrice*cutoff > 350;
15.查询id,货品名称,批发价格在300~400之间的货品
select id,productName,salePrice*cutoff from poduct where salePrice*cutoff >= 300 and salePrice*cutoff<=400;
16.查询id,货品名称,分类编号为2,4的所有货品
select id,productName,dir_id from product where dir_id = 2 or dir_id =4;
17.查询id,货品名称,分类编号不为2的所有商品
select id,productName,dir_id from product from product where dir_id != 2;
18.查询id,货品名称,分类标号的货品零售价格大于等于250或者成本大于等于200的所有商品
select id,productName,dir_id from product where salePrice >=250 or costPrice >= 200;
19.查询id,货品名称,批发价格在300~400之间的货品
select id,productName,salePrice*cutoff from product where salePrice*cutoff between 300 and 400;
20.查询id,货品名称,批发价格在不在 300~400之间的货品
select id,productPrice,salePrice*cutoff from product where not salePrice*cutoff between 300 and 400;
21.查询id,货品名称,分类编号为2,4的所有货品
select id,productName,dir_id from product where dir_id in(2,4);
22.查询id,货品名称,分类编号不为2,4的所有货品
select id,productName,dir_id from product where not dir_id in(2,4);
23.查询商品名为NULL所有的商品信息
select *from product where productName is null;
24.向当前表中插入一行数据都是NULL
insert into product(id,productName,dir_id,salePrice,supplier,brand,cutoff,costPrice)VALUES(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
25.查询id,货品名称匹配'罗技M'
select id, productName from product where productName like '罗技M%';
26.查询id,货品名称,分类编号,零售价格大于等于200的货品 或 名称能匹配'罗技M1'后两个字符的商品
select id,productName,dir_id,salePrice from product where salePrice >=200 or productName like '%罗技M1__';
27.查询 id,货品名称,分类编号,零售价格并且按零售价格升序
select id,productName,dir_id,salePrice from product order by salePrice asc;
28.查询 id,货品名称,分类编号,零售价格并且按零售价格降序
select id, productName,dir_id,salePrice from product order by salePrice desc;
29.查询id,货品名称,分类编号,零售价格按照分类编号排序降序,在按零售价格排序
select id,productName,dir_id,salePrice from product order by dir_id desc,salePrice asc;
30.查询M系列并按照批发价格排序
select * from product where productName like '%M%' order by salePrice*cutoff asc;
31.查询分类编号为2并按照批发价格排序
selcet *from product where dir_id = 2 order by salePrice*cutoff asc;
完全手打, 写了一晚上感觉要废了, 想吃烧烤啊啊啊啊啊啊啊啊啊啊啊啊