Mysql basics

Database Basics
1. There are 12 databases in the Mysql database, among which mysql is the system database, which stores information such as user access rights. (Remember not to delete).
2. Create the database statement:
   Syntax: create database database name character set character set name;
(chaaracter set uses the encoding used to set the database character set; including: big5, dec8, gb2312, gbk, etc., the system defaults to server Default); it is best to use gbk to store Chinese.
2.1. All existing databases can be viewed through show database.
       Enter the database through use databasename;
       View the current user through select use();;
2.2. You can view the character set supported by the database through show character set.
2.3. Modify the character set used by the database
       Syntax: alter database database name character set character set
2.4. Deleting the database
       Syntax: drop database database name; use show database; you can view it.
3. Database data types
3.1. Numerical type (divided into: integer type and decimal type).
3.1.1 Integer types: tingint (7 bits), smallint (15 bits), int (31 bits), bigint (63 bits).
3.1.2 Decimal type: float (4 bytes, accurate to 7 decimal places), double (8 bytes, accurate to 15 decimal places), decimal (maximum significant 65 digits, accurate to 30 decimal places) ).
3.2 String type
  char(0~255)、varchar(0~65535)、binary(0~255)、varbinary(0~65535)。
  In addition, n in char(n), varchar(n), binary(n), and bigbinary(n) can be any value within the valid range.
  char and varchar store characters, binary and bigbinary store binary data.
3.3 DateTime Type
  datatime : Storage format: YYYY-MM-DD HH:MM:SS.
  data :   Storage format: YYYY-MM-DD.
  timestamp : Displays a fixed width of 19 characters (adding "+0" to the data returns something like: 20120901111102 string).
  time : Storage format: HH:MM:SS.
4. Create the data table
     Syntax: create table custom table name (
                column name 1 data type,
                column name 2 data type,
                column name 3 data type,
             ......
            )
   Note: The maximum length of column names is 128 characters, including Chinese, English letters, underscores, ##, currency symbols (¥) and @. Duplicate column names are not allowed in the same table.
4.1 Modifying the data sheet
      Syntax: alter table table name add (modify, drop) column name;
      (add means adding a column, modify means modifying the column data, drop means deleting the data in the table).
4.2 Delete data table
      Syntax: drop table table name;
constraint
1. Primary key constraints
1.1 Add primary key constraints
      grammar:
     alter table table name add constarint pk_name primary key (column name)/(primary key (column name 1, column name 2));
     Where pk_name is a custom primary key name; the primary key can be set through the primary key (column name 1, column name 2).
1.2 Delete the primary key constraint
      Syntax: alter table table name drop primary key;
2. Foreign key constraints
2.1 设置外键约束
      语法: constraint  fk_name foreing key(列名1) refence 表名(列名2);
   fk_name : 代表外键的名字(自己定义)。
   列名1:设置外键约束所在的列。
   表名:父表的名字。
   列名2:父表中的主键列。
2.2 修改外键约束
  alter table 表名 add constraint fk_name foreing key(列名1) reference 父表名(父表主键)
2.3  删除外键约束
       语法:alter table 表名 drop foreing key fk_name;
3. 设置默认值约束
3.1 使用deafult 关键字设置默认值
  例子:
  create table student(
  studentname char(20),
  sex char(6) default '男',
  studentid char(10)
);
3.2 修改默认值约束
       语法:alter table 表名 alter 列名 set dafault 默认值;
3.3 删除默认值约束
       语法:alter table 表名 alter 存在默认值约束的列名 drop default;
4.创建非空约束(使用 NOT NULL 关键字)
4.1添加非空约束
     语法:alter table 表名 modify 列名 NOT NULL;
5.检查约束(使用check关键字)
5.1 添加检查约束
      语法:alter table 表名 add constraint 约束名 check (expression)
      expression为约束的表达式;
6. 唯一约束(使用unique 关键字)
6.1删除唯一约束
     语法: drop index 唯一约束名 on 表名;
DML数据库操作语句
1.添加数据到数据表中
1). 语法1:insert into 表名(需要添加的列名...) values (对应数据类型的有效值);
2). 语法2:insert into 表名 set 列名 = 有效值;
3). 语法3:insert into 表名(列名) select ......;
        注意: insert into 表名(列名) select ......;可以快速从一个或多个表中向一个表中插入多行数据。
        例子:将accountinfo1 表中的数据复制到 accountinfo表中。
         insert into accountinfo( id  ,  name  , account ) select *from accountinfo1 ;
2.为数据表添加多条数据
        语法:insert into 表名 values (对应值,对应值,对应值...);
3.修改数据表中的数据
        语法:update 表名 set 列名 = 有效值,...
                   [where 条件]
                   [order by ...]
                   [limit n]
       [where 条件] : 可选句,代表修改数据时的条件,不选择该语句则代表修改表中全部数据。
       [order by ...]: 可选句,代表修改数据的顺序(DESC为降序排列,ASC为升序排列)。
       [limit n]:可选句,限制可以被更新的行的数目。
4. 删除表中的数据
         语法:delete from 表名 [where 条件] [order by ...] [limit n];
       [where 条件] : 可选句,代表修改数据时的条件,不选择该语句则代表修改表中全部数据。
       [order by ...]: 可选句,代表修改数据的顺序(DESC为降序排列,ASC为升序排列)。
       [limit n]:可选句,限制可以被更新的行的数目。
简单查询与子查询
1. 比较运算符
    <、 >、 <= 、>=、<>(不等于)
    IN:判断表中的某一个字段值是否等于某一个值;
    BETWEEN AND:判断表中的某一个字段值是否在取值范围内;
    IS NULL:判断表中某一个字段值是否为NULL;
    GREATEST(greatest):返回多个值比较结果的最大值;
    LEAST(least):返回多个值比较结果的最大值;
    LIKE(like):用作模糊查询。
2.逻辑运算符
     NOT (  !  ):逻辑非 、AND(&&):逻辑与、 OR(||):逻辑或、XOP:逻辑异或。
3.简单查询
3.1 查询指定字段的数据
      语法:select 字段名 from 表名;
3.2 使用别名查询字段数据
      语法:select 字段名 as 别名 , 字段名 as 别名 ... from 表名;
3.3 单一条件查询(使用where 条件即可)
3.4 模糊条件查询(使用like关键字)
      注:“%”代表0~n个字符,“_”代表一个字符;
3.5 多条件查询数据(使用where 条件 + 逻辑运算符(NOT (  !  ):逻辑非 、AND(&&):逻辑与、 OR(||):逻辑或、XOP:逻辑异或))。
4.聚合函数
4.1 最大值函数MAX
      语法:select [(字段) ] , MAX(字段) from 表名;
4.2 最小值函数MIN
       语法:select [(字段) ] , MIN(字段) from 表名;
4.3 平均值函数AVG
       语法:select [(字段) ] , AVG(字段) from 表名;
4.4 求和函数SUM
       语法:select [(字段) ] , SUM(字段) from 表名;
4.5 计数函数COUNT
       COUNT(*):计算所有列,包括为NULL值得列。
       COUNT(列名):只计算不为NULL值得列。
       语法:select [(字段) ] , COUNT(* / (列名)) from 表名;
5.子查询
       IN:表示在某一范围。
      EXISIS:表示是否能至少返回一行数据,返回则为true,否则为false。
      ANY(SOME):表示ANY后面的结果是否至少哟一条记录与ANY前面的值匹配。
      NOT IN:表示不在某一范围内。
      NOT EXISIS:与EXISIS相反,判断的是不存在。
5.1 使用子查询
      语法:select 列名... from 表名 where 条件 (IN、EXISIS、ANY(SOME)、NOT IN、NOT EXISIS)(select ....);
6.复杂查询(重点)
6.1 分组查询(通过select语句中的group by 子句来完成)  
       例子:1).按照科目分组,计算出每个科目有几个人参加考试。
       select subject count(*) from stufentinfo group by subject;
       注:使用group by 子句时,它的select子句后面只能是聚合函数或者是group by之后的列名。
6.2 使用having 的分组查询
       having只能用在分组查询中,并且having通常放在group by子句后面。
       例子:1).查询学生成绩信息表(studentinfo),得出英语成绩的平均成绩。
       select subject,AVG(score) from studentinfo group having subject = '英语';
       注:where 的查询效率要比having的效率高;
6.3 对多列进行分组查询
      语法:select 字段、聚合函数 from 表名 group by 具体字段;
6.4 在group by 中使用order by进行排序(DESC为降序、ASC为升序)
     语法:select 字段 [聚合函数] from 表名 group by 字段名 order by [聚合函数] DESC / ASC;
7.多表查询(重点)
7.1 等值连接
      讲多个表之间的相同字段作为条件查询数据,通常是指表与表之间的主外键。
      注:对多个表进行查询时,要在select语句后面指定字段是来源于哪个表。
      select语句后面的写法是表名.列名
7.2外连接
1). 左外连接:除返回表中符合条件的结果外还要加上左表中剩下的全部记录。
2). 右外连接:除返回表中符合条件的结果外还要加上右表中剩下的全部记录。
        语法:select 列名1、列名2 ... from 表A left / right outer join 表B on 条件;
7.3 内连接:(查询所有符合条件的结果集)
      语法:select 列名1、列名2 ... from 表A inner join 表B on 条件;
7.4 合并查询结果
      使用NUION关键字合并查询结果
      语法:select 字段 from 表名 union[all] select 字段 from 表名;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324848209&siteId=291194637