初识mySQL(15)

来源:互联网 发布:樱井知香步兵番号 编辑:程序博客网 时间:2024/06/05 03:28
1.字符集和存储引擎
设置数据库字符集:
打开my.ini在[client]下增加default-character-set=utf8(或者default_character-set=utf8);
在[mysqld]下增加character_set_server=utf8;重启服务,则以后创建数据库的字符集都默认都是utf8
 
2.DML:
(1)insert:插入
       1)insert into 表名(列1,列2,列3...列n) values(值1,值2..值n)
       2)insert into 表名 values(值1,值2...值n)如果没有列名,则以为着所有的列上都要插入值,顺序按建表时列的顺序
       3)insert into 表名 values(值1,值2...值n),
         (值1,值2...值3),
         (值1,值2...值3);


4)insert into 表名(列1,列2...列n)
           select 列1,列2...列n  from 表名
      
        注意:
          主键自增
          有缺省值
          允许为Null
 对应列可以不用输入值
 
(2)delete:删除
  delect from 表名 where 条件
  truncate 表名 清空表,如果主键是自增的,编号重新从1开始
  update:update 表名 set 列名=值  where 条件
  注意:没有where,针对着一列上所有的值修改
  
(3)update:修改
  update 表名 set 列名=值,列名=值  where 条件
       注意:没有where,针对这一列上所有的值修改


3.数据库查询
  (1)单表查询:基础查询,*、常量,表达式
                select * from stars * 代表所有列,以后代码里不要用*
select name,sex,from stars;指定列
select name as 姓名 from stars;

  (2)指定字段查询(重复和不重复):
              字段别名、去除重复记录
 select distinct sex,age from stars;
 distinct 针对查询的结果集,不针对列,结果集里的记录不允许重复
 
  (3)条件查询:
关系运算、逻辑运算、字符串查询(通配符)
select * from stars where sex='男';
select * from stars where sex='女' and age <30;
select * from stars where address in('北京','香港');
    字符串模糊查询:
select * from stars where name like '_冠%';
_代表一个字符,%代表任意长度字符串
select *from stars where age>30 and age<45;
select *from stars where age between 30 and 45;
    结果集查询:
asc 升序、desc降序、多字段排序
select *from stars order by age desc;

多序列排序,先按age排序,age相同的再按address排序
select *from stars order by age desc,address desc;

限制结果集:
limit:select *from stars where address='北京' order by age desc limit 1;
//只显示结果集的第一条记录
select *from stars limit 2,3;
limit offset,n;从offset指定的记录开始显示,显示n条,结果集的记录是从0开始计数
(4)常用统计函数:
max、min、avg、sum、count、
select count(*)from stars;
select count(distinct address)from stars;
(5)分组:
分组统计:select count(*),address group by address
针对 结果集进行分组,count针对分组计数
select count(*)sex,address,from,stars group by sex,address 结果集过滤
select count(*),sex,address group by sex,address having sex='女';
select count(*)num,address group by address having num>1

where和having的区别:
where针对整个表进行过滤
having针对分组过滤   having后可以跟集合函数
整体使用:select 字段
         from 表名
 [where 条件]
 [group by]
 [having]
 [order by]
 [limit]

多表联合查询:
  连接笛卡尔积 数据没有意义
  select *from stars,salary where stars.id = salary.id select s.id,name,type,money from stars as s,salary as sa where s.id=sa.id;
 多表连接,必须写连接条件
        显示:
        select *from stars join salary on stars.id = salary.id;
        select *from 第一个表名 join 第二个表名 on 链接条件
        select *from join salary on stars.id=salary.id  where type='影展';


    三个表:select * from 第一个表名 join 第二个表名 on连接条件 join第三个表名 on链接条件
            select reader.name,book.name,borrowdate from book join borrow.rno=reader.rno;


         select sum(money)su,name from stars join salary on stars.id=salary.id group by salary.id having su>1000000;
      
    三表链接(隐式):
   select *from book,borrow,reader where book.bno = borrow.bno and reader.rno=borrow.rno;
   
    外连接:左连接:
select *from stars left join salary on stars.id=salary.id;
以左表stars为主,从中取出一条记录,拿出id,然后到右表搜索,如果有相等的id,将两条记录拼接放到结果集,如果找不到,则给右表增加一个万能的空记录表,拼接起来放到结果集
 
右连接:
 select *from stars right join salary on stars.id =salary.id;
 



0 0
原创粉丝点击