zoukankan      html  css  js  c++  java
  • 第三章 mysql 数据库接口程序以及SQL语句操作

    mysql  数据库接口程序以及SQL语句操作

    用于管理数据库:
      命令接口自带命令
      DDL:数据定义语言(create drop )
      DCL: 数据控制语言(grant revoke)
      DML: 数据操作语言(update delete insert)

    一 . mysql接口程序:

      ① mysql -uroot -poldboy123 -e "show variables like '%server_id%'"
      ② mysql>

    1. 接口自带的功能

      ① h 或help 或 ?

    help  contents;

       ② G

    mysql>  select user,host,password from mysql.userG;

      ③T  或 tee  记录操作日志

      ④ c   等于linux ctrl +c

      ⑤ s 或 status

      ⑥ . 或 source    执行外部SQL脚本:二进制日志截取、备份出来的SQL脚本

    2.查看mysql 命令帮助

    mysql> help contents;
    You asked for help about help category: "Contents"
    For more information, type 'help <item>', where <item> is one of the following
    categories:
       Account Management
       Administration
       Compound Statements
       Data Definition
       Data Manipulation
       Data Types
       Functions
       Functions and Modifiers for Use with GROUP BY
       Geographic Features
       Help Metadata
       Language Structure
       Plugins
       Procedures
       Storage Engines
       Table Maintenance
       Transactions
       User-Defined Functions
       Utility
    mysql> help data Definition;
    You asked for help about help category: "Data Definition"
    For more information, type 'help <item>', where <item> is one of the following
    topics:
       ALTER DATABASE
       ALTER EVENT
       ALTER FUNCTION
       ALTER LOGFILE GROUP
       ALTER PROCEDURE
       ALTER SERVER
       ALTER TABLE
       ALTER TABLESPACE
       ALTER VIEW
       CONSTRAINT
       CREATE DATABASE
       CREATE EVENT
       CREATE FUNCTION
       CREATE INDEX
       CREATE LOGFILE GROUP
       CREATE PROCEDURE
       CREATE SERVER
       CREATE TABLE
       CREATE TABLESPACE
       CREATE TRIGGER
       CREATE VIEW
       DROP DATABASE
       DROP EVENT
       DROP FUNCTION
       DROP INDEX
       DROP PROCEDURE
       DROP SERVER
       DROP TABLE
       DROP TABLESPACE
       DROP TRIGGER
       DROP VIEW
       RENAME TABLE
       TRUNCATE TABLE

    二 服务端命令

    1 SQL:结构化的查询语言,mysql接口程序只负责接收SQL,传送给SQL层

    2 SQL种类

          DDL:数据库(对象)定义语言 (create drop  alter )
          DCL:数据库控制语言(grant revoke)
          DML:数据(行)操作语言(update delete insert)
          DQL: 数据查询语言(show、select)

    三 DDL语句:数据定义语言 (create drop )

    DDL 操作对象分为        库   表

    1 库 的定义

      定义了: ① 库名   ②库的基本属性

    2. 定义库 定义字符集

    show databases;
    create database test CHARACTER SET utf8 ;
    create database kitty_server DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
                    
    show create database llf;
    drop database llf;
    help  create database;

    字符集: CHARACTER SET [=] charset_name
    排序规则: COLLATE [=] collation_name

    mysql> show variables like 'character_set%';
    +--------------------------+------------------------------------+
    | Variable_name            | Value                              |
    +--------------------------+------------------------------------+
    | character_set_client     | utf8                               |
    | character_set_connection | utf8                               |
    | character_set_database   | utf8mb4                            |
    | character_set_filesystem | binary                             |
    | character_set_results    | utf8                               |
    | character_set_server     | utf8                               |
    | character_set_system     | utf8                               |
    | character_sets_dir       | /data/mysql-5.6.42/share/charsets/ |
    +--------------------------+------------------------------------+

    字符集:   

      ① 服务器字符集

        控制的是,存到mysql中时,字符集控制

      ② 客户端字符集

        控制的是用户的输入及显示

      ③ 系统字符集

        控制的是系统相关的显示,和一些依赖于操作系统的应用

      

    3  修改库的字符集

    ALTER DATABASE [db_name] CHARACTER SET  charset_name COLLATE collation_name
    alter database llf charset utf8mb4 COLLATE utf8mb4_general_ci;
    show create database llf;

    4 创建表 (create)

    create table t1 (id int ,name varchar(20));

    表数据:数据行
    表属性(元数据):表名、列名字、列定义(数据类型、约束、特殊列属性)、表的索引信息

    4-1 复制表

    -- 复制表结构+记录
    create table t2_new   select * from t2;
    
    -- 复制表结构
    create table t3_new   select * from t3 where 1 =2;
    create table t3_new  like t3;

    5 删除表以及表结构 (drop)

    drop table t1;

    6  修改表 (alter)

    -- 修改表名
    alter table  table_1  rename  t2_new;
    rename table t1  to t1_new;
    
    -- 删除字段i
    ALTER TABLE table_1  DROP i;   
    
    -- 添加字段 i
    ALTER TABLE table_1 ADD i INT;
    
    -- 添加字段 i  设定位第一列
    ALTER TABLE table_1 ADD i INT FIRST;
    
    -- 添加字段 设定位于c个字段之后
    ALTER TABLE table_1 ADD i INT after c;
    
    -- 修改字段类型
    ALTER TABLE table_1 MODIFY c CHAR(10);
    
    -- change 修改字段名
    ALTER TABLE table_1 CHANGE  c c_new  CHAR(10);
    
    -- change也可以 修改字段类型
    ALTER TABLE table_1 CHANGE  c b  varchar(10);
    
    -- 删除列
    alter table table_1 drop c;
    
    -- 修改默认值为100 
    ALTER TABLE table_1 ALTER i SET DEFAULT 1000;  
    
    -- 删除默认值
    ALTER TABLE table_1 ALTER i DROP DEFAULT;
    
    -- 修改 id为主键
    ALTER TABLE table_1 modify id int(11) not null  primary key   auto_increment;
    
    -- 增加约束 (针对已有的主键增加 auto_increment)
    alter table table_1 modify id int(11) not null   primary key  auto_increment;
    
    -- 修改 id 自动增长
    alter table table_1 modify id int(11) not null auto_increment;
    
    -- 对存在的表增加复合主键
    alter  table  table_1 add primary key(host_ip, port);
    
    -- 增加主键
    alter table table_1 modify name varchar(10)  not null  primary key;
    
    -- 增加主键和自动增长
    alter table table_1 modify id int not null primary key auto_increment;
    
    -- 删除自增约束
    alter table table_1 modify id int(11) not null; 
    
    -- 删除主键
    alter table table_1 drop primary key;

    四 DML语句: 数据操作语言 (insert update delete)

    1. insert 

    insert into t1 values (2,'li4'),(3,'wang5'),(4,'ma6');
    insert into t1(name) values ('xyz');

    2. update

    ----会更新表中所有行的name字段,比较危险。
    update  t1  set name='zhang33' ;
    update  t1  set name='zhang55' where id=1;

    3. delete

    --删除表中所有行,比较危险。一行一行删除表中数据。
    delete from t1 ; 
    delete from t1   where  id=2;

    DDL  删除表  truncate

    truncate table t1;  ---在物理上删除表数据,速度比较快。

     

      

    五  DQL语句:数据查询语言(show、select)

    1. show

    show tables;
    show create table t1;
    show create database llf;

    2. 单表查询

    https://www.cnblogs.com/augustyang/p/11079174.html

    3 多表查询

    https://www.cnblogs.com/augustyang/p/11456792.html

    六 DCL语句: 数据控制语言(grant revoke)

    1.grant

    grant   all     on  ysl.*     to   test@'10.0.0.%'    identified by  '123456';
    --         权限        权限范围         用户        范文                     密码
    grant SELECT,INSERT, UPDATE, DELETE, CREATE, DROP  on  testdb.* to zabbix@'10.0.0.%'; 
    -- 创建用户并授权
    grant all on *.*  to   root@'10.0.0.%'  identified by '123456';

    2 revoke

    mysql> revoke create,drop on *.* from  ysl@'%';
    Query OK, 0 rows affected (0.00 sec)
    mysql> revoke all on *.* from  ysl@'%';
    Query OK, 0 rows affected (0.00 sec)

    七 数据类型

    https://www.cnblogs.com/augustyang/p/11079102.html

    八 完整性约束

    https://www.cnblogs.com/augustyang/p/11079165.html

  • 相关阅读:
    扑克牌大小
    简单错误记录
    聊天室
    GMM的EM算法实现
    Spark SQL 源代码分析之 In-Memory Columnar Storage 之 in-memory query
    JSP简单练习-使用JDOM创建xml文件
    PowerDesigner使用教程
    setsockopt()使用方法(參数具体说明)
    SQL注入原理解说,非常不错!
    Offer是否具有法律效力?
  • 原文地址:https://www.cnblogs.com/augustyang/p/11460162.html
Copyright © 2011-2022 走看看