MySQL中一些常用语言或方法:

DQL 数据查询语言
DML 数据操作语言
DDL 数据定义语言
TCL 事务控制语言

DQL:

SQL语言不区分大小写,但关键字通常为大

* 查询所有的内容
from 表名
select 表单

select name,stock
from books
查询指定的内容name,stock

select name as 书名,stock as 库存 from books
修改列名name as 书名,stock as 库存

select name as “书 名” from books
修改名有空格的情况

格式:
1.格式
select
*
from
books
where
id=5
2.同时查看
select
id,name,stock
from
books
where
id=5 or id=10
(或)id in(5,10)
3.同时满足用and
select
id,name,stock
from
books
where
id>3 and stock<10
查询表中空的信息:
is null 为空的信息
is not null 不为空的信息

在SQL使用单引号用来表示字符串
select
*
from
books
where
name = '妈妈'

模糊查询:
where name like '_好%'
_匹配单个字符;%匹配若干字符

升序降序:
order by price asc
order by price desc

count()计数
distinct()去重
count(distinct())去重在计数
avg平均值
sum总和

显示部分结果:
limit 4,2
开始的位置,数量

group by 归类:
select
count(id),publishedby
from
books
group by publishedby

使用having来筛选group by


DML:
插入数据
insert into students values (1,'tom',12)
插入指定列的数据
insert into students(id,name) values (1,'tom')
一次插入多行数据
insert into students(id,name) values (3,'aa'),(4,'bb')
修改数据
update students
set name='aa'
where id=4
修改多个字段的数据
update students
set name='qq',age=30
where id=1
删除数据
delete from students
where id=1

DDL:
创建表
create table teachers(
id int(10),
name varchar(100),
salary double(10,2)
)
create table 表名(
列名 列类型(长度) ,
列名 列类型(长度)
重命名表
rename table teachers2 to teachers3
重命名列(字段)
alter table teachers3
change name teachername varchar(100)
增加一个列6
alter table teachers3
add age int(10)
删除一个列
alter table teachers3
drop column age
修改列
alter table teachers3
modify teachername varchar(200)
删除一张表
drop table teachers3
清空表
truncate table students

TCL:
事务的定义:一组实现特定功能的 DML语句
特点:要么都成功,要么都失败
ACID事务的四个基本属性: 原子性,隔离性,一致性,持久性。
开启事务:
start transaction;
进行事务操作
update teachers set balance=balance-100 where id=2;
savepoint a;
update teachers set balance=balance+100 where id=1;
提交事务
commit;
事务的回滚
rollback a;
*rollback 只能回滚DML


常用到的数字函数

round(x,y) 四舍五入 ,保留y位小数
pow(x,y) 计算 x的y次方
sqrt(x) 计算 x的算术平方根
truncate(x,y) 截断数字
字符串函数:
trim(abc) 去除字符串 abc的前后空格
substring(abc,x,y abc这个字符串中从x位置取y个字符出来,生成一个新的字符串。下标从1开始
replace(abc,x,y) abc字符串中的所有的x替换为y
查看日期与日期的转换
now() 当前日期和时间
curdate() 当前日期
curtime() 当前时间
str_to_date(abc,format) abc字符串按照format给定的格式转为日期类型
date_format(date,format) date这个日期按照format给定的格式转为字符串类型。

JDBC连接数据库:
加载驱动程序:
Class.forName("com.mysql.jdbc.Driver"); 连接mysql数据库
获得数据库的连接:
Connection conn=DriverManager.getConnection(URL, USER, PASSWORD);
创建Statement对象:
Statement stmt=conn.createStatement();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值