数据库基本操作 增删查改

1数据库操作
创建:create database xxx;
使用:use xxx;
删除:drop database xxx;
显示:show databases;

2.数据类型:
数值型:
        1.整型
              bin:  计算机里二进制单位 设置boolean时,设置bit,默认长度是1,值为0和1
              int:   对应java的Integer
        2.浮点型
             float/double :   长度过长会省略
              decimal(M,D):M是总长度,D是小数位数  对精度有保证
字符串类型:
    1.varchar(size):普通的字符串使用这个
    2.text:大文本使用这个
日期类型:
    datetime:
    timestamp:对应java.util.Date
3.表的操作
创建:create table xxx{
//每一列名称(对应java的属性)
   cloumn1 type,
   cloumn2 type,
};
删除: drop table xxx;
显示表结构:desc xxx;

CRUD表示增删改查
create table student(
       sn int,
       name varchar(20),
       account decimal(11,2),
       sgroup int
);
1.新增:(insert into ...values...) 
insert into student(sn,name,account,sgroup) values
(1,'孙悟空',100.05,3);

insert into student(sn,name,account,sgroup) values
(2,'猪八戒',65.02,3),
(3,'沙和尚',78.09,3),
(4,'唐僧',34.54,2);

2.查询操作(select,from)
select * from student;//*是通配符,把所有列都写出来,类似列出所有属性
等价于 
select sn,name,account,sgroup from student;

select sn,name,account from student;//只看三项

select sn,name,account+200 from student;//字段名变了,并且值都加了200
别名:
select sn,name,account+200 acc from student; 把字段名字从account+200变成了 acc,也可以看出新写了一列
可以加as
select sn,name,account+200 as acc from student;

select sn,name,account+sn from student;//两列的值加起来
别名(结果集表头列的别名)
select sn,name,account+sn as acc from student;

DROP TABLE IF EXISTS exam_result;
CREATE TABLE exam_result (
id INT,
name VARCHAR(20),
chinese DECIMAL(3,1),
math DECIMAL(3,1),
english DECIMAL(3,1)
);


-- 插入测试数据
INSERT INTO exam_result (name, chinese, math, english) VALUES
('唐三藏', 67, 98, 56),
('孙悟空', 87.5, 78, 77),
('猪悟能', 88, 98.5, 90),
('曹孟德', 82, 84, 67),
('刘玄德', 55.5, 85, 45),
('孙权', 70, 73, 78.5),
('宋公明', 75, 65, 30);

select distinct sgroup from student;//去重 只出现3,2
select distinct sgroup,sn,name from student;//去重就没效果,还是都会出现整张表,
因为有后两个元素

3.排序(order by...asc/desc)
select * from exam_result order by chinese asc;//按chinese这个列升序排列
select * from exam_result order by chinese desc;//按chinese这个列降序排列,
不写asc,默认升序


4.条件查询
查询语文成绩80分以上的:
select * from exam_result where chinese>80;
查询在三年级的人
select * from student where sgroup=3;

查询总分
select id,name,chinese+english+math from exam_result;
总分大于200
mysql> select id,name,chinese+english+math from exam_result where chinese+english+math>200;
查询语文成绩在70到85分之间
select * from exam_result where chinese between 70 and 85;
查询学号为2和4的
select * from student where sn in(2,4);

select * from exam_result where id is null;
//打印id为null
select * from exam_result where id is not null;
//打印id不是null的

5.模糊型查询
select * from exam_result where name like '孙%';//%任意长度的匹配,_一个字符的匹配
//name的第一个字为'孙'的行
多条件查询
where 条件1 and 条件2
where 条件1 or  条件2
where 条件1 and (条件2 or 条件3)

desc exam_result  //查询表结构
order b
y...desc  降序排列

limit 3  从0开始,筛选3条结果
limit 3,2 从坐标3处开始,筛选2条结果
limit s offset n,同第二条

6.修改
updata exam_result set id=1 where name='唐三藏';
把唐三藏这行的id改为1
updata exam_result set id=1
不写后面,是把所有行的id都改为1;
updata exam_result set id=2,chinese=math-20 where name='唐三藏';
updata exam_result set id=3 where math>80 limit 2;
先弄出来where之后,在改id //从表第一行开始找,math<80的跳过,直到找够两个,

7.删除数据(delete)
delete from exam_result where id=3;
delete from exam_result;//全删


 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值