最近看了廖雪峰老师的Mysql视频,简单做个记录
基本语句
mysql
-u root
-p
show databases;
create database 数据库名
;
drop database 数据库名
;
use 数据库名
;
show tables;
create table 表名
(
......
);
创建表的示例:
create table 表名
(
id
int not null auto_increment,
name
varchar(10) not null,
primary key(id
)
);
insert语句
insert into 表名
(字段
1,字段
2...) values (数据
1,数据
2...);
insert into 表名
(字段
2...) values (数据
2...);
insert into 表名
values (数据
1,数据
2...);
select语句
select * from 表名
;
select * from 表名
where aa
=x
;
select * from 表名
where aa
=x
and bb
=y
;
select aa
,bb
from 表名
;
select count(*) from 表名
;
select count(*) number
from 表名
;
select count(*) number
from 表名
group by aa
;
select aa
,count(*) number
from 表名
group by aa
;
select * from 表名
1,表名
2;
select * from 表名
1,表名
2 inner join + 条件
;
update语句
update 表名
set aa
=x
;
update 表名
set aa
=x
where bb
=y
;
update 表名
set aa
=x
,cc
=z
where bb
=y
;
delete语句
delete from 表名
;
delete from 表名
where aa
=x
;
delete from 表名
where aa
=x
and bb
=y
;
JDBC查询
代码太多,先放一张图吧 几点说明:
Connection: 获取数据库驱动的连接对象PreparedStatement :从连接中获取sql表达式,?表示占位符,规避sql注入漏洞setObject: 将指定处的占位符替换成具体数据,索引从1开始executeQuery: 将表达式执行查询,返回一个结果集合next: 遍历每一条记录,进行处理
JDBC更新
各代码功能与JDBC查询相似 其中,更新的结果是int