黑马程序员技术交流社区

标题: 第三周01 [打印本页]

作者: wan89423    时间: 2018-12-27 16:24
标题: 第三周01
数据库的分类:
1. DDL(Data Definition Language)数据定义语言
        用来定义数据库对象:数据库,表,列等。关键字:create, drop,alter 等
操作数据库的关键字:
    创建数据库 :create database;
    查询所有数据库:show databases;
    查询指定数据库的字符集:show create database 数据库名;
    修改数据库字符集:alter database 数据库名称 character set 字符集;
    删除数据库:drop database if exists 数据库名称;
    查询当前正在使用的数据库:select database();
    使用数据库:use 数据库名称;
2. DML(Data Manipulation Language)数据操作语言
        用来对数据库中表的数据进行增删改。关键字:insert, delete, update 等
操作表的关键字:
    创建表:create table 表名;
    复制表:create table 表名 like 被复制的表名;
    查询数据库的所有表名:show tables;
    查询表结构:desc 表名;
    修改表的字符集:alter table 表名 character set 字符集名称;
    修改表名:alter table 表名 rename to 新的表名;
    添加一列:alter table 表名 add 列名 数据类型(int,varchar);
    修改列名称、类型:alter table 表名 change 列名 新列名 新数据类型;
                                  alter table 表名 modify 列名 新数据类型;
    删除列:alter table 表名 drop 列名;
    删除表:drop  table 表名;
                  drop table if exists 表名;
  3. DQL(Data Query Language)数据查询语言
        用来查询数据库中表的记录(数据)。关键字:select, where 等
增删改表中的数据:
    添加数据:insert into 表名(列名1,列名2) values(值1,值2);
    删除数据:delete from 表名  where 条件;  -- 不推荐,有多少条执行多少次删除操作
                     truncate table 表名; -- 推荐,效率更高,先删除表,然后在创建一张一样的表。
    修改数据:update 表名 set 列名1 =值1,列名2 =值2, where 条件;
查询表中的数据:
    查询所有字段(列名):select * from 表名;
    去除重复:distinct;
    计算列:select 聚合函数(列名) from 表名;
                 where 不可以跟聚合函数判断,having可以
    取别名:as
    条件查询: where之后跟条件
    berween。。。and:什么跟什么之间;
    IN(集合):查询集合里的条件;
    like:模糊查询;
           1. ?(条件)  %;
           2._?(条件) %;
          3._ _ _;
    is  null :空值不能用 !=判断




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2