码迷,mamicode.com
首页 > 数据库 > 详细

mysql常用操作

时间:2016-09-17 23:51:01      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:

一、什么是数据库

数据库(Database)是按照数据结构来组织、存储和管理数据的仓库。

SQL( Structured Query Language)语言的全称是结构化查询语言。数据库管理系统通过SQL语言来管理数据库中的数据。

SQL语言分为三个部分:数据定义语言( Data DefinitionLanguage,简称为DDL)、数据操作语言( DataManipulation Language,简称为DML)和数据控制语言( Data Control Language,简称为DCL)。

DDL语句: CREATE、 ALTER、 DROP

DML语句: update、 insert、 delete、 select
DCL语句:是数据库控制功能,是用来设置或更改数据库用户或角色权限的语句,包括( grant,deny,revoke等)语句。

二、基本数据库操作

1.创建数据库及删除数据库

 (1)create database  数据库名;

  例:create database hhf charset=utf8;

 (2)drop database 数据库名;

  例:drop database hhf;

2.创建表

 (1)创建表  create table 表名 (

    列名1 列类型 [<列的完整性约束>],
    列名2 列类型 [<列的完整性约束>],
    ... ...);

  例:   

  create table students
  (
  id int(10) not null unique auto_increment primary key,
  name varchar(20) not null,
  sex varchar(4) ,
  age int(10),
  class varchar(20) not null,
  addr varchar(50)
  );  

  

  create table score
  (
  id int(10) not null unique auto_increment primary key,
  stu_id int(10) not null,
  c_name varchar(20),
  grade int(10)
  );

 (2)删除表数据:delete  表名   删除表所有数据    truncate 表名  清空表所有数据   truncate students ;

 (3)删除表  drop table 表名    例:drop table students;

3.数据库及表的查看

 show databases; 查看系统中数据库

 show create database 数据库名;  查看数据库的相关信息

 use  databasename;选择数据库   例如:use  hhf ;

 describe tablename; 查看表结构  或者desc  tablename 

 show create table students;  查看student表结构

   flush privileges 刷新数据库

4.远程登录mysql

 mysql  -h  主机地址   -u用户名 -p用户密码   例如:mysql -h 192.168.1.119 -u root -p123456    密码和-p之间不能有空格 

  退出mysql命令:exit

3.插入数据insert语句

  (1)insert中不指定字段插入    

  insert into students values  (801,‘刘海洋‘,‘男‘,21,‘aa‘,‘北京市海淀区‘);

 (2)insert中指定字段插入

  insert into students (name,sex,age,class) values  (801,‘刘海洋‘,‘男‘,21,‘aa‘);

 (3)inser into 表名1 (属性列表1)  select  属性列表2 from表名2 where 条件表达式;

   insert into new_student select * from students;

 (4)replace插入新记录  

  replace语句的语法格式有三种语法格式。

  语法格式1: replace into 表名 [(字段列表) ] values (值列表)

  语法格式2: replace [into] 目标表名[(字段列表1)]  select (字段列表2) from 源表 where 条件表达式  

  语法格式3:replace [into] 表名set 字段1=值1, 字段2=值2

  例如:replace  into students values  (801,‘刘海洋‘,‘男‘,21,‘aa‘,‘北京市海淀区‘);

3.修改数据update语句

  update 表名

  set 属性名1=取值1, 属性名2=取值2,…,属性名n=取值n

  where 条件表达式;

  例:update students  set name=‘花花‘  where  age=18;

4.删除操作delete语句

  delete from 表名    where  条件表达式;

  例:delete from students  where age =18;

5.查询操作select语句  

  select
  {*|<字段列表>}
  [
  from <表1>,<表2>...
  [where <表达式>]
  [group by 字段名]
  [having 表达式]
  [order by 字段名]
  [limit [<offset>,]<row count>]
  ]
  select [字段1,字段2,...,字段n]
  from [表或视图]
  where [查询条件]

(1)条件查询   条件判断符=,<>,!=,<,<=,>,>=,between...and(位于两者之间)     is null、  is not null    in not in  

  select * from students  where age between 18 and 25;#查询年龄在18-25岁之间的学生信息

  select * from students  where  addr is null or addr=‘‘;#查询地址为null或者为‘‘的学生信息

      select * from students  where  id in(801,802)#查询id在801、802的学生信息

  带like的字符匹配查询
  1)百分号通配符‘ %‘,匹配任意长度的字符,甚至包括零字符
  2)下划线通配符‘_‘,一次只能匹配任意一个字符

  select * from students where name like ‘李_‘; #查询姓李名字为2个字的学生信息
  select * from students where name like ‘张%;#查询姓张的学生的信息
  select * from students where name like ‘%三%‘;#查询名字中含有‘三‘的学生信息 

(2)分组

  select * from students group by class;#以calss分组

  聚合函数:sum(),count(),avg(),max(),min()

  select avg(grade) from score; #查询score表平均分数
  select sum(grade) from score;#查询score表分数总和
  select min(grade) from score;#查询score表中分数最低的记录
  select max(grade) from score;#查询score表中分数最高的记录
  select count(*) from score;#查询score表中记录行数

(3)排序

  select * from score  order by stu_id desc ;#按照stu_id降序排序

(4)多表查询 

  多表查询是指从多张表中查询所需要的数据,一般查询的这几张表都有一个相同的字段关联这几张表。多表连接可以通过join关键字来连接,也可以直接用关联表中相同的id来进行关联;

  join:left join:左连接, 连接两张表,以左边表的数据匹配右边表中的数据,如果左边表中的数据在右边表中没有,会显示左边表中的数据。

  right join:右连接,连接两张表,以右边表的数据匹配左边表中的数据,如果左边表中的数据在左边边表中没有,会显示右边表中的数据。     

      inner join:内连接,连接两张表,匹配两张表中的数据,和前面两个不同的是只会显示匹配的数据。

  select a.name 学生姓名,b.score 学生成绩 from students a left join score b on   a.id=b.student_id;     

      select a.name 学生姓名,b.score 学生成绩 from students a right join score b on   a.id=b.student_id;

  select a.name 学生姓名,b.score 学生成绩 from students a inner join score b on  a.id=b.student_id;  

    select a.name 学生姓名,b.score 学生成绩 from students a,score b wherea.id=b.student_id;

  连接查询:
  内连接:select  表名.列名,……
             from 表1 inner join 表2   on 条件
 
  外连接:left join(左连接)
  select  表名.列名,…表2.列名……
  from 表1 left  (outer )join  表2 
  on 条件  
 
      right join(右连接)
  select  表名.列名,…表2.列名……
  from 表1 right  (outer )join  表2 
  on 条件  

(5)子查询  

  比如说要把成绩低于60分的学生的名字都改成笨蛋     

  update students set name = ‘笨蛋‘ where id in (select a.student_id from score a where a.score<60);

mysql常用操作

标签:

原文地址:http://www.cnblogs.com/wj-hua/p/5879972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!