Database Learning: the first day

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_44951165/article/details/102733220

The basic idea of ​​the database

1. English: DataBase abbreviation: DB

2. What is a database?
         For warehouse storage and management of data

3. Database Features:
     1. persistent storage of data. Database is a file system
     2. a convenience store management data
     3. Use the same way to operate the database -SQL

4. common database software
     1.Mysql
     2.Oracle

Mysql database software

     * MySQL service start
        1. Computer Management
        2.cmd-> services.msc to open the Services window

     * MySQL login
        1.mysql -uroot -proot
           password is not visible to MySQL -uroot -p if it means entering a password.
        2.MySQL -hip -uroot -p password link destination

     * MySQL exit
        1.exit

SQL

1. What is SQL?
     structured Query Language: Structured Query Language
     is actually defines all the rules relational database operations. Existence of each database operation is not the same place, called "dialects."

2.SQL universal grammar

     1.SQL statement written in single or multiple rows, end with a semicolon.
     2. Use a space or indentation to enhance the readability of the statement
     3.MySQL database SQL statements are not case sensitive, use uppercase keyword suggestion
     4. There are three kinds of comments
          * Single-line comments: - Comment # contents or footnotes
                mysql> SHOW dATABASES; - View all database names
          * multi-line comments: / * comment * /
                MySQL> SHOW dATABASES; / * Check all the database name * /

3.SQL classification

          DDL (Data Definition Languages) statement: data definition language defines the different pieces of data, databases, tables, columns, indexes, and other database objects Keywords: create, drop, alter

          DML (Data Manipulation Language) statements: data manipulation statements to add, delete, update and query the database record and checks data integrity keywords: insert, delete, update and select, etc.

DDL

1. The operation of the database: CRUD

     1.C (Create): Creating
          CREAT DATABASE name
         create database if not exists name
          * Create a database to determine whether there is, and specify the character set is utf8
          create database if not exists the Test Character the SET utf8;

     2.R (Retrieve): Query
          * SHOW DATABASES query names of all databases;
      * query the database default character set SHOW CREATE DATABASE MYSQ;

     3.U (Update): Modify

          * Modify the character set of the database
                * alter database database name of the character set and character set;

     4.D (Delete): Delete
          * Delete the database
                * drop database database name;
                * drop database IF EXISTS database name;

     5. Use Database
          * Query the database name currently in use
               * the SELECT Database ();
          * use the database
               * use the database name;

2. Operation Table
     1.C (Create): Create
          1. Syntax:
                Create table Table
                {
                column names 1, a data type,

                }
          2. Data type
          age int, score double (5,2) , data type

     2.R (Retrieve): Query
          * Query all table names
               * Show the Tables;
          * lookup table structure, header
               * desc table;

     3.U (Update): modify
      1. Modify table
        alter table table name rename to the table name
      character set table 2. Modify
        alter table show SET UTF8 Character
      3. Add an
        alter table table add Column Name Data Type
      4. Modification column name type
        alter table table name change column names new column name data type
      5. delete the column
        alter table table name drop column names

     4.D (Delete): Delete

          * drop table 表名;
          *drop table if exisys 表名;

DML

***1.添加数据***
   
   *  语法
     *  insert into 表名(列名1,列名2,....列名n) values(值1,值2,...值n);
     *  注意
       1. 列名与值一一对应 
       2. 如果表名后,不定义列名,则默认给所有列添加值
           insert into 表名 values(值1,值2,...值n);
       3. 除了数字类型,其他类型需要使用引号(单双均可)引起来
     
***2.删除表中数据***
   
   * 语法
      * delete from 表名 [where 条件]; 
      * 注意
           1.如果不加where条件,则删除所有数据
           2.truncate table 名字   --- 删除表。然后再创建一个一模一样的表,没有数据
           
***3.修改数据***

   * 语法
      * update 表名 set 列名1 = 值1,列名2 = 值2,....[where 条件];
   * 注意
     *  如果不加where条件,则会修改所有记录 

DQL: lookup table records

  *   -----------***1. 语法***--------------------------------
  *       select
  *           字段列表
  *       from
  *           表名列表
  *       where
  *           条件列表
  *       group by
  *           分组字段
  *       having
  *           分组之后的条件
  *       order by 
  *           排序
  *       limit
  *           分页限定
  * 
  * 
  *  ---------***2. 基础查询***----------------------------------
  * 
  * 
  *    1.多个字段的查询
  *         select 字段名1,字段名2,....from 表名
  *          eg:select name,age from student;
  *         * 注意
  *              如果查询所有字段,则可以用*来代替字段列表
  *    2.去除重复
  *         distinct
  *         eg:select distinct address from student;
  *    3.计算列
  *         一般可以使用四则运算计算列值,只会进行述职型计算
  *        ifnull(表达式1,表达式2);
  *          * 表达式1 哪个字段需要判断为null
  *          * 表达式2为null后的替代值
  *    4.起别名
  *       * as 也可省略
  *       select name,math as 数学 from student;
  * 
  * 
  *  --------- ***3.条件查询***------------------------------
  * 
  *
  *  1.where 子句后跟条件
  *  2.运算符
  *    >  <  <=  >=  =  <>//不等于 
  *      select *from student where age > 20;
  *    between...and
  *      select *from student where age between 20 and 40;
  *    in(集合)
  *      select *from student where age in(20,40);
  *    like(模糊查询)
  *       * _ :表示单个任意字符
  *             select *from student where name like '_华';  --云华,辉华
  *       * % :表示多个任意字符
  *             select *from student where name like '马%';
  *    is null
  *      select *from student where english is null;
  *    and 或 &&
  *      select *from student where age > 20 and age < 40;
  *    or 或 ||
  *      select *from student where age = 20 or age = 40;
  *    not 或 !
  *      select *from student where english is not null;

Guess you like

Origin blog.csdn.net/weixin_44951165/article/details/102733220