Sql完成对表中数据的增删改查操作--插入数据insert into 表名(列名1,列名2,列名3) values(值1,值2,值3);insert into student(sid,sname,ge
Sql完成对表中数据的增删改查操作
--插入数据
insert into 表名(列名1,列名2,列名3) values(值1,值2,值3);
insert into student(sid,sname,gender,age,shengao) valuse(1,'张三','男',18,170);
简单写法
insert into student values(1,'张三',18,170);  如果只插入其中部分列,则不可以省略列名
--批量插入
insert into student values(1,'张三',18,170),(2,'张1',19,155),(...); 
--单条插入和批量插入的效率问题
单条效率<批量插入
--插入中文字符乱码问题
将mysql安装路径中的my.ini配置文件中 第57行编码改成gbk
--删除记录
delete from 表名 【where 条件】
delete from student where sid=10;
delete from student; 如果没有指定条件会把表中数据全部删除
delete 跟 truncate 删除数据区别
delete:一条一条删除表中数据
truncate:DDL 先删除表 再重建表 (更改了表的结构)
关于删除效率问题:具体要看表中的数据量 量少 delete 量大 truncate
--更新表记录
update 表名 set 列名=列的值,列名2=列的值2 【where条件】
update student set sname=&#39;张宁&#39; where sid=1;(如果不加where限制条件,所有人都将改成张宁)
如果update 主键 列 必须加判定条件
--查询表中数据
select * from student;
select 【distinct】【*】【列名,列名2】from 表名【where条件】
distinct:去除重复的数据
--商品分类:手机数码,鞋靴箱包
1.分类的ID
2.分类名称
3.分类描述
create table category(
cid int primary key auto_increment,
cname varchar(10),
cdesc varchar(30)
) ;&#160; &#160; &#160; &#160; &#160; &#160; &#160; 如果遇到Duplicate column name &#39;cname&#160; 解决办法set&#160; names bgk;
--所有商品
insert into category values(null,&#39;手机数码&#39;,&#39;电子产品&#39;);
insert into category values(null,&#39;鞋靴箱包&#39;,&#39;日用品&#39;);
insert into category values(null,&#39;香烟酒水&#39;,&#39;奢侈品&#39;);
select * from category;
select cname ,cdesc from category;
--所有商品
1.商品ID
2.商品名称
3.商品价格
4.生产日期
5.商品分类ID
商品跟商品分类 :所属关系
create table product(
pid int primary key auto_increment,
pname varchar(10),
price double,
pdate timestamp,
cno int&#160;
);
insert into product values(null,&#39;小米mix4&#39;,998,null,1);
insert into product values(null,&#39;锤子&#39;,2898,null,1);
insert into product values(null,&#39;阿迪&#39;,99,null,2);
insert into product values(null,&#39;玉溪&#39;,20,null,3);
--简单查询
-- 查询所有商品
select * from product;
--查询商品名称和商品价格
select pname ,price from product;
--别名查询:as 关键字 as 关键字可以替换
--表别名:select p.pname,p.price from product as p;(主要是用于多表查询)
--列别名:select pname as 商品名称,price as 商品价格 from product; (as 可以省略)
--去掉重复的值
--查询商品所有的价格
select price from product;
select distinct price from product; (distinct 去重)
--select运算查询【在查询结果上进行处理】
select * ,price *1.5&#160; from product;
select *,price*1.5 as 折后价 from product;
--条件查询【where关键字】
指定条件,确定要操作的记录
select * from product where price > 60;
--where 后的条件写法
<>&#160; &#160;不等于&#160; &#160; 标准sql语法
!=&#160; 不等于&#160; 不标准sql语法
--查询商品价格不等于88的商品
select * from product where price !=88;
--查询商品价格在10-100之间
select * from product where price >10 and price <100;
between ... and ...
select * from product where price between 10 and 100;
--逻辑运算
and or not
select * from product where price <100 or price >900;
--like:模糊查询
_:代表一个字符
%:代表多个字符
--查询第二个字是米的商品
select * from product where pname like &#39;_米%&#39;
--查询商品ID是 1 2 4的商品
select * from product where cno in (1,2,4);
&#160;
&#160;
&#160;
&#160;
&#160;
&#160;
&#160;
&#160;
&#160;
&#160;