Home  >  Article  >  Database  >  What are the operations related to Mysql?

What are the operations related to Mysql?

一个新手
一个新手Original
2017-09-30 09:59:021986browse

1》Create database:
Syntax: create database database name;
Syntax: show databases View existing database
Example:

 Mysql->create database zytest;    注意每一条要以;号结尾
    Mysql->show databases;查询是否创建成功
        >use zytest;

2》Delete database:
Syntax: drop database database name; 582b545076095af5efdcb440c8d66f85
Example:
Mysql->drop database zytest; Delete zytest
Mysql-> show databases; Query whether the deletion is successful

The table provides transaction logs, rollback, crash, repair capabilities and transaction safety of multi-version concurrency control. Mysql includes the innoDB storage engine starting from 3.23.34a. InnoDB is the first table engine to provide foreign key constraints and supports innoDB's transaction processing capabilities. It is also something that other engines cannot compete with. ,

InnoDB Support the automatic growth column using Auto_increment, the automatic growth column cannot be empty for the air. The table is the parent, and the fields in the parent table that are associated with the child table must be the primary key. When a piece of information in the parent table is deleted or updated, the child table must also change accordingly.

In the innodb storage engine, create a table The table structure is stored in the .frm file, and the data and indexes are stored in the table space defined by innodb_data_home_dir and innodb_data_file_path. By default, it is under datadir. The metadata of each data table in InnoDB is always stored in the shared table space ibdata1, so this file is essential.

innodb_data_file_path = ibdata1:10M:autoextend



Data and index files are gathered together: *.ibd Each table has a separate metadata,

Table definition file: *.frm

Total metadata for all tables The file is ibdata1 Advantages of the Inoodb storage engine: it provides good transaction management, crash, repair capabilities and concurrency control, Disadvantages: its reading and writing efficiency is slightly poor, and the data space it occupies is relatively small Big.




What is a transaction? ? Let’s first take a look at the ACID principles
ACID is the four basic elements for the normal execution of database transactions, which refer to atomicity, consistency, independence and durability
Atomicity

:

The atomicity of a transaction means that a transaction is either fully executed or not executed. In other words, a transaction cannot be executed only halfway and then stop. For example, if you withdraw money from an ATM, this transaction can be divided into two Steps: 1. swipe the card, 2. withdraw money. It is impossible to swipe the card but the money does not come out. These two steps must be completed at the same time. Otherwise, it is not completed.

       Consistency:
The consistency of the transaction means that the running of the transaction does not change the consistency of the data in the database. For example, the integrity constrains a+b=10, and a transaction changes a, then b should also change accordingly. .
                               :The independence of a transaction is also called isolation, which means that two or more transactions will not have interleaved execution
status. Because this may lead to data inconsistency.   Durability:
The durability of a transaction refers to the changes made by the transaction to the database after the transaction is successfully executed. Changes are permanently saved in the database and will not be rolled back for no reason.

2>MyISAM engine
The MyISAM storage table is divided into 3 files. The file and table names are the same, and the extensions include frm, MYD and MYI.
The structure of the file storage table where frm is the extension
Myd Store data for files with extension
  Myi stores index for files with extension
Advantages: Small space occupied. Fast processing speed,
Disadvantages: Does not support the integrity and concurrency of transaction logs 3>MEMORY engine The special engine in Mysql, all data is stored in the memory, in the enterprise production environment. Almost useless. Because the data is stored in memory, if there is an exception in the memory. Will affect the integrity of the data. Advantages: Fast storage

Disadvantages: Lack of stability and integrity

MyISAM: does not support foreign keys, does not support transactions, indexes and data are separated, and more can be loaded Index, and the index is compressed, which improves the usage efficiency a lot compared to memory. It uses a table locking mechanism to optimize multiple concurrent read and write operations. MYISAM emphasizes fast read operations;
Use Occasion: In a project platform that hosts most projects that read more and write less, the read performance of MyISAM is much better than Innodb

## Innodb: supports foreign keys , supports transactions and rollback, but the index and data are tightly bound, and no compression is used, which will cause INNODB to be much larger than MYISAM.
Usage occasions: If you want to perform insert and update on most projects hosted, you should choose InnoDB.

Introduction to locks: There are three common lock levels in MySQL - table-level locks, page locks, and row-level locks; among them, table-level locks have two modes - —Table shared read lock and table exclusive write lock.

MyISAM: Table-level lock : When performing a read operation on the myisam table, it will not block other users' read requests for the same table, but it will block write operations on the same table;       When performing a write operation on the myisam table, it will block Read and write requests from other users to the same table.


## innodb:

Provides row locks (locking on row level). In addition, the row locks of InnoDB tables are not absolute. If MySQL is executing a SQL statement If the range to be scanned cannot be determined, the InnoDB table will also lock the entire table.## The advantages of row-level locking are as follows:

 1) Reduce the LOCK status when many connections perform different queries respectively.

  2) If an exception occurs, data loss can be reduced. Because you can roll back only one row or a few rows of small amounts of data at a time. The disadvantages of row-level locks are as follows: 1) It takes up more memory than page-level locks and table-level locks.
 2) Querying requires more I/O than page-level locks and table-level locks, so we often use row-level locks for write operations rather than read operations.
  3) Deadlocks are prone to occur.
Note: inodb cannot determine the row of the operation. At this time, the intention lock is used, that is, the table lock on row level);




4》View storage engine: Storage engine is a feature of Mysql. Mysql can choose multiple storage engines and different storage methods. Whether to perform transaction processing, etc.; ​ ​ 1> Query the engines supported by Mysql ​ ​ ​ ​ Mysql->show engines;


#                                                            

# #                                                                                                                                                                                                                                

2>Query Mysql engine details:            

 Mysql->show engine innodb status\G;

      3>查询Mysql默认存储引擎           

Mysql-> show variables like 'storage_engine';

        如果想修改存储引擎,可以在 my.ini中进行修改或者my.cnf中的Default-storage-engine=引擎类型;

                  

5》如何选择存储引擎

           在企业生产环境中,选择一个款合适的存储引擎是一个很复杂的问题。每一种存储引擎都有各自的优势,不能笼统的说,谁比谁好。通常用的比较多的     是innodb存储引擎
    以下是存储引擎的对比:

         

==========================创建,修改,删除表:

1》创建表的方法:
       语法:create table 表名(
    属性名数据类型完整约束条件,
    属性名数据类型条完整约束件,
    。。。。。。。。。
    属性名数据类型
  );

  举例:    

create table example0(
          id int,
          name varchar(20),
          sexboolean);

2》表的完整性约束:

       | 约束条件                              | 说明|

| (1)primary key      | 标识该字段为表的主键,具备唯一性|

| (2)foreign key       | 标识该字段为表的外键,与某表的主键联系|

| (3)not null             | 标识该属于的值不能为空|

| (4)unique              | 标识这个属性值是唯一|

| (5)auto_increment      | 标识该属性值的自动增加

| (6)default          | 为该属性值设置默认值|

        1>设置表的主键:
               主键是一个表的特殊字段,这个字段是唯一标识表中的每条信息,主键和记录的关系,跟人的身份证一样。名字可以一样,但是身份证号码觉得不会一样,            主键用来标识每个记录,每个记录的主键值都不同,主键可以帮助Mysql以最快的速度查找到表中的某一条信息,主键必须满足的条件那就是它的唯一性,表中的          任意两条记录的主键值,不能相同,否则就会出现主键值冲突,主键值不能为空,可以是单一的字段,也可以多个字段的组合。

    举例:   

 create table sxkj(
        User_id int primary key,
        user_name varchar(20),
        user_sexchar(7));

         2>设置多个字段做主键
   举例:   

 create table sxkj2(
        user_id int ,
        user_name float,
        grade float,
          primary key(user_id,user_name));

            3>设置表的外键:
             外键是表的一个特殊字段,如果aa是B表的一个属性且依赖于A表的主键,那么A表被称为父表。B表为被称为子表,
         举例说明:
              user_id 是A 表的主键,aa  是B表的外键,那么user_id的值为zhangsan,如果这个zhangsan离职了,需要从A表中删除,那么B表关于                               zhangsan的信息也该得到相应的删除,这样可以保证信息的完整性。
    语法:
        constraint外键别名 foreign key(外键字段1,外键字段2)
                   references 表名(关联的主键字段1,主键字段2) 

                    (1) yy1表存储了zhangsan姓名和ID号
           create table yy1(
               user_id int primary key not null,
               user_name varchar(20));

                         

                        (2) yy2表存储了ID号和zhangsan的年龄(old)              

 create table yy2(
                    user_id int primary key not null,
                    old int(5),
                    constraint y_fk foreign key(user_id)
                    references yy1(user_id)on delete cascade on update cascade);


                                 

                     (3)数据填充yy1和yy2表            

 insert into yy1 values('110','zhangsan');
              insert into yy2 values('110','30');


                               

                   (4)更新测试:

           update yy1 set user_id='120' where user_name='zhangsan';
         查询验证
           select * from yy2;


                              

                    (5)删除测试:          

delete from yy1 where user_id='120';
        查询验证
          select * from yy2;


                              

            4>设置表的非空值
             语法:属性名数据类型  NOT NULL
             举例:              

 create table C(
                  user_id int NOT NULL);

            5> 设置表的唯一性约束
              唯一性指的就是所有记录中该字段。不能重复出现。
             语法:属性名数据类型  unique
             举例:              

 root@zytest 15:43>create table D(
                                                      ->user_id int unique);
                root@zytest 15:44>show create table D;

             6>设置表的属性值自动增加
                 Auto_increment 是Mysql数据库中特殊的约束条件,它的作用是向表中插入数据时自动生成唯一的ID,一个表只能有一个字段使用                                       auto_increment 约束,必须是唯一的;
            语法:属性名数据类型 auto_increment,默认该字段的值从1开始自增。
            举例:
              

create table F( user_id int primary key auto_increment);
               root@zytest 15:56>insert into F values();插入一条空的信息
               Query OK, 1 row affected, 1 warning (0.00 sec)
               root@zytest 15:56>select * from F;值自动从1开始自增
               +---------+
               | user_id |
                +---------+
              |       1 |
              +---------+
              1 row in set (0.01 sec)

               7>、设置表的默认值
                 在创建表时,可以指定表中的字段的默认值,如果插入一条新的纪录时,没有给这个字段赋值,那么数据库会自动的给这个字段插入一个默认                      值,字段的默认值用default来设置。
               语法: 属性名数据类型 default 默认值
               举例:                 

 root@zytest 16:05>create table G(
                           user_id int primary key auto_increment,
                           user_name varchar(20) default 'zero');
                   root@zytest 16:05>insert into G values('','');

                  插入数据,应为ID为自增,值为空,user_name设置了默认值,所以也为空。

3》查看表结构的方法:
            DESCRIBE可以查看那表的基本定义,包括、字段名称,字段的数据类型,是否为主键以及默认值等。。
         (1)语法:describe 表名;可以缩写为desc
         (2)  show create table查询表详细的结构语句,
        1>修改表名
               语法:alter table 旧表名 rename 新表名;
               举例;                

 root@zytest 16:11>alter table A rename zyA;
                  Query OK, 0 rows affected (0.02 sec)

        2>修改表的数据类型
              语法:alter table 表名 modify 属性名 数据类型;
              举例;                

root@zytest 16:15>alter table A modify user_name double;
                 Query OK, 0 rows affected (0.18 sec)

       3>修改表的字段名称
             语法: alter table 表名 change 旧属性名 新属性名 新数据类型;               

  root@zytest 16:15>alter table A change user_name user_zyname float;
                 Query OK, 0 rows affected (0.10 sec)

                 4>修改增加字段                

 alter table 表名 ADD 属性名1  数据类型 [完整性约束条件] [FIRST |AFTER 属性名2]

      v      增加没有约束条件的字段:              

  root@zytest 16:18>alter table A add phone varchar(20);
                 Query OK, 0 rows affected (0.13 sec)

      v       增加有完整约束条件的字段                

root@zytest 16:42>alter table A add age int(4) not null;
                 Query OK, 0 rows affected (0.13 sec)

      v 在表的第一个位置增加字段默认情况每次增加的字段。都在表的最后。                

root@zytest 16:45>alter table tt add num int(8) primary key first;
                 Query OK, 1 row affected (0.12 sec)
                 Records: 1  Duplicates: 0  Warnings: 0

      v          执行在那个位置插入新的字段,在phone后面增加                 

 root@zytest 16:46>alter table A add address varchar(30) not null after phone;
                   Query OK, 0 rows affected (0.10 sec)
                   Records: 0  Duplicates: 0  Warnings: 0

            总结:
             (1)  默认ADD 增加字段是在最后面增加
             (2)  如果想在表的最前端增加字段用first关键字
             (3)  如果想在某一个字段后面增加的新的字段,使用after关键字
    5>删除一个字段
          alter table 表名DROP 属性名;
          举例: 删除A 表的age字段            

root@zytest 16:51>alter table A drop age;   
             Query OK, 0 rows affected (0.11 sec)
             Records: 0  Duplicates: 0  Warnings: 0  

    6>更改表的存储引擎           

  alter  table表名 engine=存储引擎
              alter table A engine=MyISAM;

    7>删除表的外键约束            

 alter table 表名drop foreign key 外键别名;
              alter table  yy2  drop foreign key  y_fk;

  4》删除表的方法

        1>删除没有被关联的普通表
               drop table 表名;
        2>删除被其它表关联的父表
               在数据库中某些表之间建立了一些关联关系。一些成为了父表,被其子表关联,要删除这些父表,就不是那么简单了。删除方法,先删除所关联的                子表的外键,在删除主表。

The above is the detailed content of What are the operations related to Mysql?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:MySql index operationNext article:MySql index operation