zoukankan      html  css  js  c++  java
  • sql 入门经典(第五版) Ryan Stephens 学习笔记 (第一,二,三,,四,五章)

    SQL - Structured  Query Language (结构化查询语言)

    1/ SQL 命令的类型 :

    数据定义语言: DDL

    数据操作语言: DML

    数据查询语言: DQL

    数据控制语言: DCL

    数据管理命令

    事物控制命令

    2/ 数据库 - 表

    主键: 确保所有元素的标识都是唯一的

    不同的表的映射: 公用某个字段(通常是主键)

    3/ mysql for windows 安装 官网  http://dev.mysql.com/downloads/installer

      选择安装 developer default 版本的 

      安装过程中需要为 root用户设置密码 (其他选择默认)

          将路径  C:Program FilesMySQLMySQL Server 5.7in (mysql.exe的路径)  添加到系统环境变量中

      (选择安装)mysql 可视化管理工具 Navicat 

    4/ 通过 mysql command line 访问远程 mysql (直接输入密码) 可在所有程序中查找

     通过 cmd(管理员权限登陆) mysql -uroot -p 登陆

       简单命令: 

     >  show databases ;   查看sql中所有的数据库

     >  use a0;                   切换到 a0这个数据库当中 ,操作某个数据库之前都必须先 use 该数据库

       >  show tables;            查看a0中的表 

       > desc stu_info;           查看a0中stu_info表中的 字段 及 属性  结构

       > select * from stu_info  

       ->where stu_name = "zlj";  在表中查找 stu_name = "zlj"  的成员

      >grant all on a0.* to "cool"@"localhost" identified by "123456" ;        创建一个 cool用户 密码是123456 , 他只能访问 a0 数据库,而不能访问其他数据库   在该账号下,只能看到 a0数据库,而不能看到 其他数据库

      5/ 创建数据库 , 以及对数据库的简单操作

         > create database a1;

         > use a1; 

      5.1/创建数据表的一般操作:

      create [temporary] table [if not exists] tbl_name [([column_definition],...|[index_definition])] [table option][select_statement]; 

      temporary : 不加 ,则表示是持久表 。 否则为临时表,只能对创建它的用户可见,当断开与数据库连接时,mysql会自动删除临时表

      if not exist : 建表前,判断该表名是否已经存在。  //  create table if not exists student(id int(10) primary key auto_increment);

      column_definition :  列定义,包括 列名/数据类型,可能还包括空值声明和一个完整性约束

      index_definition :     表索引项定义,主要定义表的索引/主键/外键

      table option      :   用于描述表的选项

      select statement  : 在一个表的基础上建立一个表

       5.2/ colum_definition 的定义格式

      col_name type [not null | null] [default default_value] [auto_increment] [unique[key] | [primary] key] [comment 'string'] [reference_definition]

      type; 列的数据类型,有的数据类型需要指明长度,并用括号括起来

      not null | null : 指定该字段 是否允许为空,如果不指定 默认为 null

      default default_value : null  , 如果是not null  0   // alter table test add column class int(30) default 10;    设置默认值为10的class信息

      unique key  |  primary key :  表示字段中的值是唯一的, 但是 primary key 只能有一个 ,而且一定为not null 

      comment ‘string’  : 对于列的描述

      5.3/ 修改数据表:alter 命令

      alter [ignore] table table_name alter_specification ;

    alter_specification:
    ADD [COLUMN] column_definition [FIRST | AFTER column_name ]                     
    or ADD INDEX [index_name] (index_col_name,...)
    or ADD PRIMARY KEY (index_col_name,...)
    or ADD UNIQUE [index_name] (index_col_name,...)
    or ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}         //设置或删除列的默认值(操作速度非常快)

    or CHANGE [COLUMN] old_col_name create_definition //列的重命名、列类型的变更以及列位置的移动

    or MODIFY [COLUMN] create_definition //除了不能给列重命名之外,他干的活和CHANGE COLUMN是一样的
    or DROP [COLUMN] col_name                                                // 从表中删除列或者约束
    or DROP PRIMARY KEY or DROP INDEX index_name 
    or RENAME [AS] new_tbl_name or table_options
    eg:

     add [column]  : 向列表中 增加新列  

       column_definition : 定义列的数据类型和属性  

       FIRST | AFTER column_name : 列的前 或 后添加 ,不指定则添加在最后

        ALTER [COLUMN]:

        

      >  create table student(id int(10) primary key auto_increment , name varchar(30), age tinyint(2));

     创建一个 student表, 包含id  name age  三个属性 ,以及他们的类型

      > drop table student ;                                                     // delete table student

      >desc student;                                                               //查看此时表的结构  其实是: describe student ;

      >insert into student (name,age) values("zhangsan",22);   //插入一个张三的用户到表中  或者直接: insert into student values ("zhangsan",22);

      >select * from student;                                                  //可以看到 所有成员

      >alter table student modify id int(20);                            // 改变表中的数据类型

      > alter table student add birday date;                             // 增加字段,birday 类型为 date 类型

         >  update student set birday ="1992/09/16" where id=2; // 不加条件则把所有的记录都更新

         > quit;

        转到cmd中:

      >  mysqldump -uroot -p a1>d:/a1.sql                          // back up table file into a1.sql in d:

        >   mysql -uroot -p a1<d:/a1.sql                                 // restore the database a1 in back up file

      >  mysql -uroot -p  a2<d:/a1.sql             // put the tables in a1 to database a2  

       在command line中时 ;

      >  use a2 ;

      > source d:/a1.sql                                                    // same as "mysql -uroot -p  a2<d:/a1.sql "

    6/ mysql 数据类型

    系统类型: http://www.cnblogs.com/im5437/articles/5515200.html

    用户自定义类型:

     >create type person as object

    (name  varchar(30),

      ssn     varchar(30));

    >quote self definition type as follow:

     create table emp_pay(employee person,

                salary decimal(10,2),

                hire_data data); 

    7/ select 用法

      select name, id from student ;                  // 显示 name 和 id ,而不现实其他字段

      select *from student order by age limit 3;   // 按照age从低到高排序输出, 只输出前三条 , 其实省略 aesc

      select *from student order by age desc limit 3;  //从高到低进行排序 

      select *from student order by age desc limit 3,2;  // 从第四条开始输出,输出第 4 ,5 条 

      select name ,age from student where age>=(select age from student order by age limit 1,1);

                            // 输出年龄前两位的人, 考虑到第二位和第三位年龄相同时,也输出第三位

                             // 注意后半部分只选择出 age字段

        select year(birday) from student ;              // 只输出date 类型的birday 中的year

         select distinct year(birday) as "学生出生年份" from student ;  // 输出不重复的 年份 

    8/ 外键约束

    外键约束 是确保表与表之间引用的完整性,一个被定义成外键的字段用于引用另一个表里的主键。

    代码
    CREATE TABLE product (
    
                          category INT NOT NULL,
    
                          id INT NOT NULL,
    
                          price DECIMAL,
    
                          PRIMARY KEY(category, id)
    
    ) TYPE=INNODB;
    
     CREATE TABLE customer (id INT NOT NULL,
    
                          PRIMARY KEY (id)
    
    ) TYPE=INNODB;
    
     CREATE TABLE product_order (no INT NOT NULL AUTO_INCREMENT,
    
                          product_category INT NOT NULL,
    
                          product_id INT NOT NULL,
    
                          customer_id INT NOT NULL,
    
                          PRIMARY KEY(no),
    
                          INDEX (product_category, product_id),
    
                          FOREIGN KEY (product_category,product_id) 
    
                          REFERENCES product(category, id)
    
                          ON UPDATE CASCADE ON DELETE RESTRICT,
    
                          INDEX (customer_id), 
    
                          FOREIGN KEY (customer_id) 
    
                          REFERENCES customer(id)
    
    ) TYPE=INNODB;

    作用:要想在子表product_order中插入一个product的值时,product的值必须可以在父表product中能够找得到。

    类似的,父表里面删除一个 product时,字表里面相应的product也必须删除。

      InnoDB允许你用ALTER TABLE往一个表中添加一个新的外键约束:

    ALTER TABLE yourtablename

        ADD [CONSTRAINT symbol] FOREIGN KEY [id] (index_col_name, ...)

        REFERENCES tbl_name (index_col_name, ...)

        [ON DELETE {RESTRICT | CASCADE | SET NULL | NO ACTION}]

        [ON UPDATE {RESTRICT | CASCADE | SET NULL | NO ACTION}]

       记住先创建需要的索引。你也可以用ALTER TABLE往一个表添加一个自引用外键约束。

    InnoDB也支持使用ALTER TABLE来移除外键:

    ALTER TABLE yourtablename DROP FOREIGN KEY fk_symbol;

    mysql中实际例子:

    // 将ord表中的cumtomer_id列设置为外键
    // 关联 info 表中的id字段
    // 当插入的customer_id字段在info中id找不到时会报错
    order table ord 
        add foreign key(customer_id) references info(id) on update cascade on delete cascade; 

    附注: alter的用法总结

    http://blog.csdn.net/ws84643557/article/details/6939846

    mysql中的变量:

    系统变量: @@开头 

      @@global.xxxxx    全局系统变量的值

      @@session.xxxx    会话系统变量的值

    用户自定变量 @开头

    如查看sql模式的命令: 

      select @@global.sql_mode ;   

      select @@sql_mode;

      select @@session.sql_mode;

         

      

      

  • 相关阅读:
    单链表的基本操作--c++
    剑指Offer-- 替换空格
    华为机试题-字符串分隔
    Leetcode 98. Validate Binary Search Tree
    树的层次遍历的几种方法
    Camera Path插件的使用
    3d 人物残像
    gameUnity 0.15 beta 网络游戏框架
    gameUnity 0.15alpha 网络游戏框架
    gameUnity 网络游戏框架
  • 原文地址:https://www.cnblogs.com/NeilZhang/p/5517124.html
Copyright © 2011-2022 走看看