zoukankan      html  css  js  c++  java
  • mysql笔记(1)

    DDL语句是数据定义语言的缩写,(创建、删除、修改)
    和DML语句的区别:DML语句是对表的内部数据进行操作,而不涉及表的定义,结构的修改,更不会涉及其他对象。DDL语句更多的由数据库管理员使用!

    DDL语句:
    一、创建数据库
    1.create database 数据库名 ;//创建数据库
    2.show databases ; //显示数据库
    3.use 数据库名; //选择数据库
    4.show tables; //显示数据库中的表名

    二、删除数据库
    drop database 数据库名;//删除数据库

    三、创建表

    1.create table 表名(
    列的名称1 数据类型1 约束条件1,
    列的名称2 数据类型2 约束条件2,
    列的名称3 数据类型3 约束条件3
    )

    
    create table emp(
        ename varchar(10),
        hiredate date,
        sal decimal(10,2),
        deptno int(2)
    );
    
    create table dept(
        deptno int(2),
        deptname varchar(10)
    );

    2.desc 表名 G;//查看表的定义

    四、删除表
    drop table 表名;//删除表

    五、修改表
    1.修改表类型
    alter table 表名 modify 列的名称 修改为

    eg:alter table emp modify ename varchar(20);

    2.增加表字段
    alter table 表名 add column 增加为、

    eg :alter table emp add column age int(3);

    3.删除表的字段名
    alter table 表名 drop column 删除的列名字

    eg:alter table emp drop column age;

    4.字段改名
    alter table 表名 change 原来名字 改后的名字 ;

    eg:alter table emp change age age1 int(4);

    注意:change和modify都可以修改表的定义,不同的是change后面需要写2次列名,不方便。但是change 的优点是可以修改列的名称,modify则不能。

    5.修改字段排列顺序
    ADD增加的字段默认在表的最后的位置,而change/modify默认不会改变字段的位置(first/after)
    eg:将新增的字段birth date 加在 ename之后

    alter table emp add birth date after ename;


    eg:修改字段age,并将其放在第一个

    alter table emp modify age int(3) first;


    eg:修改字段的位置

    alter table emp modify age int(3) after ename;


    注意:change/first/after column 这些关键字都属于mysql在标准SQL的扩展,在其他数据库是上不一定支持!

    6.更改表名
    alter table 表名 rename 修改的表名


    DML语句:
    DML操作是指对数据库中表记录的操作,(insert、update、delete、select)是开发人员用的最多的操作。

    1,插入记录
    insert into 表名 (file1,file2,file3) values (value1,value2,value3);

    insert into emp(ename,hiredate,sal,deptno) 
    values('zzx1','2000-01-01','2000',1);

    可以不指定字段,但是其后面的值要和字段的顺序是一样的。

    insert into emp values('th','2017-12-12','2017',2);


    也可以只写某些字段,没有写的字段和值默认为NULL

    insert into emp (ename ,sal) values('dony',1000);


    一次性插入多条记录
    insert into 表名 (file1,file2,file3) values (value1,value2,value3),(value1,value2,value3);

    insert into emp (ename ,sal) values('dony',1000),('jack',999);

    总和:

    insert into emp (ename ,hiredate,sal,deptno) values('zzx','2000-01-01','2000','1'),
    ('lisa','20003-02-01','4000','2'),('bjguan','2004-05-01','5000','1'),('bzshen','2005-04-01','4000','3');
    insert dept (deptno,deptname) values(1,'teach'),
    (2,'sale'),(3,'hr');

    2.更新记录
    update table set file1 = value1,file2 = value2..filen=valuen[where condition]

    update emp set sal=4000 where ename='lisa';


    更新多个表的数据
    update t1,t2,...tn set t1.file1=expr1,t2.file2=expr2.. [where condition]
    例如:同时更新emp中的字段sal和demo字段中的deptname;

    update emp ,demo set emp.sal=1000,demo.deptno=2 where 
    emp.deptno = demo.deptno;


    另一种表达方式

    update emp a,demo b set a.sal=2000,b.deptno=1;
    update emp a,demo b set a.sal=a.sal*b.deptno,b.sal = a.sal where
    a.deptno = b.deptno;


    (a和b类似表的别名)
    注意:多表更新的语法更多的用在根据一个表的字段来动态的更新另外一个表的字段。

    3.删除记录
    delete from 表名 [where conditon]

    delete from emp where ename='dony';


    mysql还可以删除多行数据
    delete t1,t2,..tn frmo from t1,t2,..tn [where condition]

    delete a,b from emp a,dept b where a.deptno=b.deptno
    and a.deptno=3;

    3.查询记录
    (全部查询)
    select * from 表名 [where condition]

    select * from emp;//*表示所有的记录
    select ename,hiredate from emp;//查询特指定的列

    (查询不重复记录)

    select distinct deptno from emp

    (条件查询)

    select * from emp where deptno=1;


    除了=之外还有>、<、>=、<=、!=等运算符,多个条件查询可以用or、and等逻辑运算符号进行多条查询
    select * from emp where deptno=1 and sal < 3000;

    (排序和限制)
    select * from 表名 [where condition] [order by file [desc/asc],file2[desc/asc]];
    desc为:降序排序
    ASC :表示升序排序

    select * from emp order by sal;


    还可以对查询的行数进行限制
    select ..[limit offset_start,row_count]
    offset_start表示偏移量,row_count表示行数

    revoke insert on sakila.* from 'th'@'localhost';


    如果想从第二个开始查询可以

    select * from emp order by sal limit 1,3;

    注意:limit 和order 经常用于分页用

    (聚合)
    应用条件:用于统计公司人数、部门人数。
    p41
    //求部门的总人数

    select count(1) from emp;

    //统计各个部门的人数

    select deptno ,count(1) from emp group by deptno;

    //统计部门人数和总人数

    select deptno,count(1) from emp group by deptno having count(1)>1;

    //统计所员工的最低收入和最高收入

    select sum(sal),max(sal),min(sal) from emp;

    (表连接)
    分类为内连接和外连接
    例如查询所有雇员的名字(内连接)

    select ename,deptname from emp,dept where emp.deptno=dept.deptno;

    外连接(左连接和右连接)
    左连接:包含所有的左边表中的记录甚至是右边表中没有和它匹配的记录
    右连接:包含所有的右边表中的记录甚至是左边表中没有和它匹配的记录。

    select ename,deptname from emp left join dept
    on emp.deptno=dept.deptno;(左连接)


    比较和上面一个连接之间的区别:
    最大的区别在于(左连接)即使不存在某个部门,用户dony也会显示,但是上面那个就不会显示了

    select ename,deptname from dept right join emp
    on dept.deptno=emp.deptno;(右连接)

    (子查询)
    某些情况下,进行查询的时候,需要条件是另一个select语句的结果,这个时候就需要用到子查询

    select * from emp where deptno in
    (select deptno from dept);


    如果子查询记录唯一可以用=代替in

    select * from emp where deptno =(select deptno
    from dept limit 1); 


    在某些情况下,子查询可以转化为表连接

    select * from emp where deptno in(select depnto
    from dept);

    注意:子查询和表连接之间的转换主要应用在2个方面
    1.mysql4.1以前的版本不支持子查询
    2.需要用到表连接来实现子查询的功能

    (记录联合)
    将2个表的数据按照一定的查询条件查询出来后,将结果合并在一起
    (union union all)
    union all:是把所有结果集直接合并在一起
    union:将union all的结果进行distinct,去除重复记录后的结果。

    select deptno from emp union all select deptno from dept;
    select deptno from emp union select deptno from dept

    DCL语句用来管理系统中的对象权限的使用,一般开发人员很少使用
    创建一个用户,给它一定的权限

    grant select ,insert on sakila.* to 'th'@'localhost'
    identified by '123';

    收回th insert 的权限

    revoke insert on sakila.* from 'th'@'localhost';


    帮助的使用
    1,帮助的总目录? contents
    2.快速查阅帮助
    ? show
    假如想查询create tale的语法
    ? create table就可以了

  • 相关阅读:
    eclipse p2更新官网wiki的例子
    打包完的rcp产品svn不储存密码问题
    hudson 使用节点打包出现ClassNotFoundException: org.jvnet.hudson.maven3.agent.Maven3Main 错误
    maven 安装第三方jar到本地 出现 The goal you specified requires a project to execute but there is no POM in this directory 错误
    利用tycho插件自动生成pom文件
    蒙特卡洛(Monte Carlo)方法求面积
    笨办法学Python(learn python the hard way)--练习程序42
    笨办法学Python(learn python the hard way)--练习程序41
    笨办法学Python(learn python the hard way)--练习程序39-40
    笨办法学Python(learn python the hard way)--练习程序31-35
  • 原文地址:https://www.cnblogs.com/healy/p/6798452.html
Copyright © 2011-2022 走看看