zoukankan      html  css  js  c++  java
  • SQL学习笔记

    这篇笔记是记录基于ubuntu平台的mysql学习过程

    一.安装Mysql

    1.首先检查系统中是否已经安装过mysql服务

    $ sudo service mysql start

    如果未安装

    安装mysql服务端、核心程序

    $ sudo apt-get install mysql-server

    安装mysql客户端

    $ sudo apt-get install mysql-client

    2.查看是否启动成功

    $ sudo netstat -tap | grep mysql

    当出现

     说明启动成功

    3.如果需要修改mysql的配置文件,则用:

    sudo gedit /etc/mysql/my.cnf

    4.初始化基本配置

    $ sudo mysql_secure_installation

    可按下方方法配置

    #1
    VALIDATE PASSWORD PLUGIN can be used to test passwords...
    Press y|Y for Yes, any other key for No: N (我的选项)
    
    #2
    Please set the password for root here...
    New password: (输入密码)
    Re-enter new password: (重复输入)
    
    #3
    By default, a MySQL installation has an anonymous user,
    allowing anyone to log into MySQL without having to have
    a user account created for them...
    Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (我的选项)
    
    #4
    Normally, root should only be allowed to connect from
    'localhost'. This ensures that someone cannot guess at
    the root password from the network...
    Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y (我的选项)
    
    #5
    By default, MySQL comes with a database named 'test' that
    anyone can access...
    Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (我的选项)
    
    #6
    Reloading the privilege tables will ensure that all changes
    made so far will take effect immediately.
    Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y (我的选项)

    5.检查mysql的状态

    $ systemctl status mysql.service

    如果显示为

     说明启动成功

    6.登录

    使用命令登入数据库

    $ sudo mysql -uroot -p

    二.建表

    7.进入成功后,可以先查看一下预置的数据库有哪些

    $ show databases;     

    这里的分号不能漏掉

    8.选择所要操作的数据库

    $ use 数据库名;

    9.查看数据库中有哪些表

    $ show tables;

    10.退出

    quit或者exit

    11.创建新的数据库和数据表

    进入mysql之后,创建新的数据库

    $ CREATE DATABASE myfirst_sql;

    然后进入这个数据库

    $ use myfirst_sql;

    创建新的数据表

    创建的时候要按照格式:CREATE TABLE 表名(第一列名 第一列的数据类型(第一列的数据长度),第二列名 第二列的数据类型(第二列的数据长度),第三列名 第三列的数据类型(第三列的数据长度),......)

    $ CREATE TABLE employee(id int(10),name char(20),phone int(12));

    或者用更整齐的格式:

    $ CREATE TABLE employee

    --> (

    -->id int(10),

    -->name char(20),

    -->phone int(12)

    -->);

    效果是一样的,看起来更直观

     12.为新的数据表添加数据,主要用insert into语句

    按照格式:INSERT INTO 表的名字(第一列名,第二列名,第三列名) VALUES(第一列此行中的值,第二列此行中的值,第三列此行中的值);

    也可以使用简化的格式

    1 INSERT INTO employee(id,name,phone) VALUES(01,'Alice',123456789);
    2 
    3 INSERT INTO employee VALUES(03,'Bob',234567890);
    4 
    5 INSERT INTO employee(id,name) VALUES(02,'Julie');

    其中要注意的是第三行中没有添加对应列数据的,相应的位置会自动填充NULL

    并且,数据类型中需要加单引号修饰的是varchar,text,date,time,enum等,数字类的一般不用加单引号

     13.修改表中的数据,假如当前有表格如图所示

     

     需要修改第一行的内容,那么用的是update语句

    mysql > UPDATE Student

             -> SET Sname='Zhaolei',Ssex='M'

             -> WHERE S=01;

    添加成功

     三.查询

     1 #筛选一个/多个列的信息
     2 SELECT column, another_column, …
     3 FROM tablename;
     4 
     5 #查看所有信息
     6 SELECT *
     7 FROM tablename;
     8 
     9 #根据条件查询
    10 SELECT column, another_column, …
    11 FROM mytable
    12 WHERE condition
    13     AND/OR another_condition
    14     AND/OR …;

     14.常用的条件有

    Operator Condition SQL Example
    =, !=, < <=, >, >= Standard numerical operators col_name != 4
    BETWEEN … AND … Number is within range of two values (inclusive) col_name BETWEEN 1.5 AND 10.5
    NOT BETWEEN … AND … Number is not within range of two values (inclusive) col_name NOT BETWEEN 1 AND 10
    IN (…) Number exists in a list col_name IN (2, 4, 6)
    NOT IN (…) Number does not exist in a list col_name NOT IN (1, 3, 5)

     15.模糊搜索的条件

    Operator Condition Example
    = Case sensitive exact string comparison (notice the single equals) col_name = "abc"
    != or <> Case sensitive exact string inequality comparison col_name != "abcd"
    LIKE Case insensitive exact string comparison col_name LIKE "ABC"
    NOT LIKE Case insensitive exact string inequality comparison col_name NOT LIKE "ABCD"
    % Used anywhere in a string to match a sequence of zero or more characters (only with LIKE or NOT LIKE) col_name LIKE "%AT%"
    (matches "AT", "ATTIC", "CAT" or even "BATS")
    _ Used anywhere in a string to match a single character (only with LIKE or NOT LIKE) col_name LIKE "AN_"
    (matches "AND", but not "AN")
    IN (…) String exists in a list col_name IN ("A", "B", "C")
    NOT IN (…) String does not exist in a list col_name NOT IN ("D", "E", "F")
     1 #结果去重
     2 SELECT DISTINCT column, another_column, …
     3 FROM mytable
     4 WHERE condition(s);
     5 
     6 #结果排序
     7 SELECT column, another_column, …
     8 FROM mytable
     9 WHERE condition(s)
    10 ORDER BY column ASC/DESC;
    11 
    12 #限制结果(limit取限制条件内的,offset是筛选经过limit筛选后剩下的,如limit 2 offset 2 就是指第三第四)
    13 SELECT column, another_column, …
    14 FROM mytable
    15 WHERE condition(s)
    16 ORDER BY column ASC/DESC
    17 LIMIT num_limit OFFSET num_offset;

    16.当有多个表格时

    1 #用inner join on 把两个表格连接在一起,连接点要找两个表格中的公共数据
    2 SELECT column, another_table_column, …
    3 FROM mytable
    4 INNER JOIN another_table 
    5     ON mytable.id = another_table.id
    6 WHERE condition(s)
    7 ORDER BY column, … ASC/DESC
    8 LIMIT num_limit OFFSET num_offset;

    17.涉及到多个表格时,有left join,right join等,会有点迷,对它的理解可以参考下图

     1 #寻找一个表格中的空白数据
     2 SELECT column, another_column, …
     3 FROM mytable
     4 WHERE column IS/IS NOT NULL
     5 AND/OR another_condition
     6 AND/OR …;
     7 
     8 #寻找联表中的空白数据
     9 SELECT column, another_column, …
    10 FROM mytable
    11 LEFT/RIGHT JOIN anothertable
    12 ON mytable.keyword =anothertable.keyword
    13 WHERE column IS/IS NOT NULL
    14 AND/OR another_condition
    15 AND/OR …;

    18.对于表格中的列,如果需要一些数学运算得出结果生成新的列数据

    1 SELECT particle_speed / 2.0 AS half_particle_speed
    2 FROM physics_data
    3 WHERE ABS(particle_position) * 10.0 > 500;
    4 
    5 #常用的 + - * / %

    一些常用的聚合表达式

    Function Description
    COUNT(*), COUNT(column) A common function used to counts the number of rows in the group if no column name is specified. Otherwise, count the number of rows in the group with non-NULL values in the specified column.
    MIN(column) Finds the smallest numerical value in the specified column for all rows in the group.
    MAX(column) Finds the largest numerical value in the specified column for all rows in the group.
    AVG(column) Finds the average numerical value in the specified column for all rows in the group.
    SUM(column) Finds the sum of all numerical values in the specified column for the rows in the group.

    把产生的各个数据分组,用GROUP BY

    19.关于having语句

    1 SELECT group_by_column, AGG_FUNC(column_expression) AS aggregate_result_alias, …
    2 FROM mytable
    3 WHERE condition
    4 GROUP BY column
    5 HAVING group_condition;

    一般与GROUP BY语句一起使用,以允许我们从结果集中过滤分组的行

     HAVING语句约束的编写方式与WHERE语句约束的编写方式相同,并应用于分组的行

    20.修改/更新数据

    1 UPDATE mytable
    2 SET column = value_or_expr, 
    3     other_column = another_value_or_expr, 
    4 5 WHERE condition;

    SET中的内容是需要修改的内容,不必把这一行的内容都重新写一遍,只需把要修改的地方写出来即可

    注意:如果忘记加WHERE,那么表格的目标列整个内容都会被修改掉

    21.删除数据

    1 DELETE FROM mytable
    2 WHERE condition;

    22.添加列和修改数据表的表名

    1 ALTER TABLE mytable
    2 ADD column DataType OptionalTableConstraint 
    3     DEFAULT default_value;
    4 
    5 #修改数据表的名称
    6 ALTER TABLE mytable
    7 RENAME TO new_table_name;

    23.删除整个表格

    $ DROP TABLE mytable

    参考文章:https://blog.csdn.net/weixx3/article/details/80782479

     https://blog.csdn.net/u014204541/article/details/79739980

  • 相关阅读:
    验证码图片不刷新解决方法
    表单验证
    Thinkphp显示系统常量信息的方法(php的用法)
    原生sql语句执行
    Python中的模块(2)
    Python 正则表达式中级
    正则表达式 和 原生字符串 r
    collections模块
    时间模块
    random模块
  • 原文地址:https://www.cnblogs.com/RuiRuia/p/12937692.html
Copyright © 2011-2022 走看看