[jdbc连接mysql数据库]mysql数据库入门学习笔记

更新时间:2019-07-14    来源:php入门    手机版     字体:

【www.bbyears.com--php入门】

数据库一直没怎么重视,前段时间看了看mysql的基础知识,不看不知道,一看吓一跳,很多基础都竟然不知道,一直傻傻的用一些简单的、笨笨的方法,看了之后原来竟是如此如此,生活如此多娇,以前看不懂的,现在也懂点了,以前看到就头晕的,现在不晕了,发现一个奇怪的现象,应该很多人都有吧,当学一种知识的时候,而当时确实又是学不会、学不好的时候,随着时间的慢慢推移,再回过头来看的时候,发现比以前容易接受得多了……难怪这么多人到快挂的时候才后悔,不扯这么多了,把记录的笔记分享出来,方便日后查悦。

一、data数据文件夹文件说明

.frm    表字段
.MYD    表数据
.MYI    表索引

二、mysql常用命令

SELECT VERSION();                   //数据库版本号
CURRENT_DATE();                    //数据库日期
SELECT (20+5)*4 AS result;    //通过AS关键字设置字段名
SELECT USER();                        //当前登录用户和数据库地址
SELECT NOW();                        //当前日期时间
EXIT;QUIT;                                //退出
SHOW DATABASES;               //显示当前存在的数据库
USE guest;                                 //选择一个数据库
SELECT DATABASE();          //查看当前所选择的数据库
SHOW TABLES;                     // 查看有多少张表
DESCRIBE guest;                  //显示表结构
SET NAMES gbk;                 // 根据数据库设置中文编码
create database                     //创建数据库
alter database 原数据库名 modify name=新名字    //更新数据库名称
drop database book;            //删除数据库
create table user();              //创建一张表
drop table user;                   //删除表
show create database test;    //查看数据库字符集
show create table user;      //查看表字符集
show index from t2;           //查看表中的所有索引
//复制表结构+复制表数据
create table t3 like t1;                      //创建t3表,并将t1表的结构复制到t3
insert into t3 select * from t1;     //复制t1的数据到t3表

三、mysql常用数据类型

整数型:TINYINT(常用),, SMALLINT, INT(常用), BIGINT
浮点型:FLOAT, DOUBLE, DECIMAL(M,D)(常用)
字符型:CHAT(常用), VARCHAR(常用)
日期型:DATETIME, DATE, TIMESTAMP
备注型:TINYTEXT, TEXT(常用), LONGTEXT
字段属性:
unsigned                          //无符号,全是正数
zerofill                             //零填充,int(2),不够2位补0
auto_increment            //自增长
null                                 //这一列值允许为null
not null                          //这一列值不允许为null
default                           //默认值
primary key                  //主键索引
index in_named(name)         //普通索引

 

四、数据表操作

数据表维护:
alter table t1 add age int;                                                              //添加字段
alter table t1 modify age int not null default 20;                     //修改字段属性
alter table t1 drop age;                                                                  //删除字段
alter table t1 change name username varchar(30);                //修改字段名
alter table t1 add index in_name(name);                                 //添加普通索引
alter table t2 drop index index_name;                                     //删除普通索引
show index from t2;                                                                     //查看表中的所有索引
新增数据:insert into table(username) values(‘哈哈’);
更新数据:update table set username=’笑笑’ where id = 1;
删除数据:
单条删除:delete from table where id = 1;                     //把ID=1的删除了
批量删除:delete from table where id in(1,2,3);            //删除id等于1,2,3的数据
delete from t1 where id between 1 and 3;
delete from t1 where id >= 3 and id <= 5;

五、SQL查询语句

1.选择特定的字段
select id,name from user;
2.给字段取别名-as
select id,name from user;
select id maoxian,name from user;
select id as maoxian,name from user;
3.distinct关键字的使用,取出唯一值,把重复值去掉,只能单列
select distinct age from user;
4.使用where 条件进行查询
select * from user where id >= 3 and id <= 5;
5.查询空值null
select * from user where age is null;
select * from user where age is not ull;
6.between and 的使用方法
select * from user where id between 3 and 5;
7.in 的使用方法
select * from user where id = 1 or id = 2 or id = 10;
select * from user where id in(1,2,10);        //建议使用这个
8.like的使用方法
//模糊查询,text类型不能加索引        % 匹配所有        _ 匹配一个字符       % 在前,name这一列的索引会失效
select * from user where name like “%mysql%”;
9.使用order by 对查询结果排序,asc 和 desc ,一个升序,一个降序
select * from user order by id asc;    //默认就是升序,数字从小到大
select * from user order by id desc;    //默认就是升序,数字从大到小
10.使用limit限定输出个数(分布实现)limit 0,5 前五条数据
select * from user order by id limit 0,2;
select * from user order by id limit 5;
11.conact 函数 – 字符串连接符
select concat(‘a’,"-’,"b’);
12.rand()  随机函数,随即抽取
select * from user order by rand() limit 3;
13.count 记录统计
select count(id) tot from user;
select count(*) tot from user;    //最快,推荐
select count(id) from user where name=’user4′;
14.求和
select sum(id) from user;
select sum(id) from user where name=’asd’;
15.avg平均数
select avg(id) from user;
16.max最大值
select max(id) from user;
16.min最小值
select min(id) from user;
17.group by 分组聚合的使用,只分组没有意义,必须用函数去聚合
//统计每个班人数和总成绩
select clsnum,count(id),sum(id) from classtab group by clsnum;
select name,count(id) tot from group by name order by tot desc;
//group by 必须写在order by 之前,having要在group by 之后,having是对分组后的结果进行筛选
18.多表查询
1.普通多表查询-多表(优先选择)
select user.name,post.title,post.content from user,post where user.id=post.uid;
select user.name,count(post.uid) from user,post where user.id=post.uid group by post.uid;
表字段之间用.连接
2.左链接查询-多表
select user.name,post,title from user left join post on user.id=post.uid;
3.子查询/嵌套查询-多表
select name from user where id in(select uid from post);
六、mysql帮助手段
? show;         ? select;
?  命令  , 即可查看该命令的使用帮助
检测sql语句
desc select * from user where id=1;         //一行显示
desc select * from user where id=1G;    //列表对应显示
//rows 1         代表影响行数
暂毕。

本文来源:http://www.bbyears.com/jiaocheng/58037.html

热门标签

更多>>

本类排行