邓_ Mysql·笔记

  MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司。MySQL是最流行的关系型数据库管理系统,在WEB应用方面MySQL是最好的RDBMS(Relational Database Management System:关系数据库管理系统)应用软件之一。

--------------------------------------------------------------------------------------

=============================================================================

truncate table dongfang_hk 清除所有数据,主键从1开始
delete from dongfang_hk 删除所有数据,主键继续增长

 =============================================================================

连接数据库
①:mysql -hlocalhost -uroot -p
②:mysql --default_character_set=latin1 -uroot

--查询数据库
show databases;
--使用数据库
use job
--创建表
create table 表名 (
->列1 列类型 [列属性 默认值],
->列2 列类型 [列属性 默认值],
->......
);

[char(10); 定长varchar(10); 变长]
[primary key 主键索引]
[unique key 唯一索引]
[auto_increment 自增长]
[default 0]
[not null 不能为空]


eg:
create table t_001(
id int primary key auto_increment,
name varchar(50),
age int(20)>charset=utf8
);

--插入数据
①:insert into t_001 values(1,'zhangsan','22');
②:insert into t_001 (id,name) values(2,'zhaosi');

--查询表结构
desc t_001;

--①查询表数据
select * from t_001;
select * from t_001 where id=1;

--②修改表数据

update 表名 set 数据 where 条件;

eg:
update t_001 set age=22 where id=2;
--③删除表数据[v:drop>truncate>delete]
delete from 表名 where 条件;

eg:
delete from t_001 where id=2;

1:取出100<=价格<=500的商品(不用and)
select * from 表名 where 字段名 between 100 and 500;
2:取出栏目id号等于4或11的商品(不用or) or=in
select * from goods where 字段 in (4,11);
3:取出价格大于100且小于300,或者大于4000且小于5000 的商品
select * from 表名 where (字段)100 and 字段(300) or (字段)4000 and 字段(5000);
group分组与统计函数
count()计算行数
avg()求平均函数
sum()求总和
min()求最小
max()求最大
order by 字段
[asc 升序][desc 降序]
limit(限制):[跳过几条,取几条]
取前3名 limit 0,3
取前3到前5名 limit
left join
select * from 表1 left join 表2

eg:
select avg(字段) from 表名;
where:——>原表所对应的列
having:——>经过运算改名后,(处理结果集)
eg:
select name,sum(score<60) as gk,avg(score) from 表名 group by 字段 having gk>2;

--------------------------------------------------------------------------------------------------邓总随笔

原文地址:https://www.cnblogs.com/vip-deng-vip/p/6946723.html