MySQL common operations

Today, I accidentally saw an article that wrote a better database and it stuck.

Reprinted from: https://blog.csdn.net/qq_21397217/article/details/51656783 (each small knowledge point)

https://blog.csdn.net/zdwzzu2006/article/details/2298777 (integration of small knowledge points is clear)

https://www.cnblogs.com/dinglinyong/p/6656315.html ( Seven types of MySQL joins )

main content:

  • Start the MySQL service
  • Login as root user
  • View command help information
  • View security permissions granted to users
  • Notes
  • Cancel the command being entered
  • See which databases are currently included
  • Connect to the database
  • View which tables are contained in the current database
  • show table properties
  • Display server warning or error messages
  • Exit the database
  • new database
  • New data table
  • type of data
  • insert data
  • SQL constraints
  • MySQL wildcards
  • MySQL query statement keyword order
  • SELECT operation
  • delete database
  • Rename data table
  • delete data table
  • add column to table
  • delete column
  • Modify column
  • Modify data type
  • Modify a value in a table
  • delete a row
  • index a column
  • show the index of a table
  • Create a view of a virtual table
  • Import saves the data in the file into a table
  • Export saves the data in the table to a file
  • Backup the entire database
  • backup the entire table
  • restore database
  • regular expression
  • whitespace metacharacter
  • Character class predefined character set
  • match multiple instances
  • positioning metacharacters
  • splice field
  • alias alias
  • MySQL arithmetic operators
  • SELECT test
  • SQL functions
  • Commonly used text processing functions
  • Date and time handling functions
  • Numerical processing functions
  • SQL aggregate functions
  • packet data
  • SELECT clause order
  • subquery
  • 1. Create a basic table
  •     create table <table_name> (<column_name><datatype>[column-level integrity constraints]

                                                         [,<column name><data type>[column-level integrity constraints]

                                                         .........

                                                         [,<table-level integrity constraints>]);

           2. Modify the basic table

                 alert table <table name>

                                      [add <new column name> <datatype> [integrity constraint]]

                                      [drop <integrity constraint name>]

                                      [modify <column name><data type>];

              add: used to add a new column and the integrity constraints related to the new column to the table, the new column will not carry any data after it is successfully added

              drop: used to remove the specified integrity constraints

              modify: used to modify the original column definition, including modifying the column name and data type, the original data may be lost after the column data type is modified

     

    alert table student add  Scome date;
    alert 
    table student modify Sage smallint
    ;
    alert 
    table student dropunique(Sname); 

             3. SQL does not provide the function of deleting attribute columns. If you need to delete a column, you must do it indirectly through a select statement, copy the content of the required column to another table, delete the original table, and then name the new table. original name

            4. Delete the basic table

                  drop table <表名>

           5. Create and delete indexes

                create [unique] [cluster] index <index name> on <table name> (<column name> [<order][,<column name>[<order]].....);

                Note: unique: indicates that each index value of this index corresponds to only one record

                        cluster: The index to be established when the table is a clustered index, that is, after the index is built according to a certain column, the data storage order on the hard disk is also adjusted to be stored in the order of this column, and the order of the index is consistent with the storage order

                 drop index <index name>;

     

    createuniqueindex studentIndex on    student (sno);
    dropindex studentIndex; 

    2. Data query

           select  [distinct | distinctRow | all]

                        columnname[, columnname][, ....]

                        [into {outfile | dumpfile} 'filename' export_options]

                        [from table name]

                        [where query condition]

                       [group by columnName[, ....]]

                        [having condition definition]

                       [order by {unsigned integer|column_name|formula}[asc|desc],...]

                      [limit [offset,]lines]

                      [procedure procedure name]

                   Note: All keywords used in the select statement must be given in the exact order above 

          If the order is in descending order, the keyword desc is added to the order by, and the keyword asc is added to the ascending order. The default is ascending order.

          The having subname can refer to any column or alias named in the query, it is used last, just before the item is sent to the client, with no change. Be careful not to use having for items that should be in the where clause.

                           limit子名被用来限制select返回的行数,如果给定1个参数,它指出返回行的最大数目;如果给定2个参数,第一个指定要返回的第一行的偏移量,第二个指定返回行的最大数目,初始行的偏移量是0

    // 返回前5个记录
    select * from table limit 5
    ;

    //
    返回第6到第15个记录
    select * from table limit 5,10;

                        select .... into outfile '文件名' 格式的select语句将选择的行写入一个文件,文件在服务器上被创建,并且不能是已经存在的,且在服务器主机上还必须有file权限以使用这种select

                        distinct,使用了这个关键字后,再执行select语句的结果集中,如果有重复的值,则会在结果集中去掉重复的值而只保留一个值。

    // 选择学生表中所有学生的姓名记录,并且筛选掉重复的记录,将记录保存在名字为name的结果集中,并按学号由大到小排列
    select distinct sname as name, sno from student order by sno desc
    ;

    //
    选择学生表中所有学生的姓名和年龄,并将每个同学的姓名后加上“同学”二字,将每个人的年龄增加1岁,同是计算所有同学年龄的平均值 
    // sum() ---- 求所有数据的总和

    // max() ----求所有数据中的最大值
    // min() -----求所有数据中的最小值
    // count() ---求所有数据的个数
    // avg() ---- 求所有数据的平均值
    select sname, "同学", age+1avg(age) from  student;

    //
    列出学号在1和100之间,并且成绩大于85分的所有姓”赵“同学的所胡记录
    // % ---代表任意字符

    //使用通配符时,前面必须用关键字”like
    select * from student where sname like '
    赵%’ and sno between 1 and 100 and sno in (select sno from student where grade>85);

    //使用学生表、课程表以及学生选课表这3个基本表,列出所有学生的姓名、学号、所选课程的课程名称及该课程的学分
    select sname, sno, cno, credit from student, course, sc where student.sno=sc.sno and sc.cno=course.cno;



    三、数据更新

    1、数据插入

       insert into <表名> [<列名>[,<列名>]....) values (value1, value2,...);

                 如果表中的每一列均有数据插入,则可不用指定任何表名

    2、数据修改

       update <表名> set <列名>=新值 where <条件表达式>

    3、数据删除

               删除指的是删除数据库中的一个记录,而不是删除某一列

               delete from <表名> where <条件表达式>

                       


Guess you like

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