Learn more about the basic operations of Mysql
1. Table copy
1. Copy the table structure Copy the structure of table hello to table hello3
2. Copy data
a. If the structure of the two tables is the same and you want to copy all columns Data
mysql> insert into hello3 select * from hello;
b. The two table results may be different and you only need to copy the data of some columns
mysql> insert into hello3 (name,sex,degree) select name,sex,degree from hello;
2.Index
1. create creates an index (only ordinary indexes and unique indexes can be created)
Create an ordinary index: mysql> create index in_name on hello(name); to the table The name column in hello creates an index named in_name.
Create a unique index: mysql> create unique index un_name on hello(name); Create a unique index named un_name for the name column in the hello table.
View index: mysql> show index from hello; View the index of table hello.
Delete index: mysql> drop index in_name on hello; Delete the index named in_name in the hello table.
2. Alter creates an index (a common way to create an index)
Create a normal index: mysql> alter table hello add index in_name(name); Create an index named in_name for the name column in the hello table.
Delete (ordinary/unique) index: mysql> alter table hello drop index in_name; Delete the ordinary index named in_name in the hello table.
Delete auto-increment: mysql> alter table hello modify id int unsigned not null; delete the auto-increment feature of the id column (int type) in table hello.
Delete the primary key index: mysql> alter table hello drop PRIMARY KEY;
Create a unique index: mysql> alter table hello add unique(name); Create a unique index index name for the name of the hello table is the default.
Create a primary key index: mysql> alter table hello add primary key(id); Create a primary key index for the id field of the hello table.
Set the primary key index to auto-increment: mysql> alter table hello modify id int unsigned not null auto_increment; Set the primary key id column in the hello table to auto-increment.
3. View
If the main table data changes, the view will make corresponding changes from time to time. If the table that the view depends on has an error (is deleted) then the view will also have an error.
Create view: mysql> create view v_hello as select * from hello where id >5;
Delete view: mysql> drop view v_hello;
View the creation process of the view: mysql> show create view v_hello; View the creation process of the view v_hello.
4. Built-in functions
Character functions
1. CONCAT(str1,str2, ....) Character link function
Mysql> select concat('A','B' );
2. LCASE(str1) Convert to lower case
mysql> ; select lcase("MYSQL");
3. UCASE(str1) Convert to uppercase
mysql> select UCASE("Mysql");
4. LENGTH(str ) The length of str
Mysql> select length('mysql');
5. LTRIM(Str) removes the preceding spaces
mysql> select LTRIM(' mysql') ;
6. RTRIM(str) remove back-end spaces
mysql> select RTRIM(' mysql ');
7. REPEAT(str,count) repeat count times
mysql> select repeat('mysql',2);
8. REPLACE(str,search_str,replcae_str) Replace search_str in str with replac_str
mysql> select REPLACE('mysql','m','M');
9. SUBSTRING(str,postion,length) Take length characters starting from the postion of str
mysql> select substring('mysql',1,2); Starting from 1
10. SPACE(count) generates count spaces
mysql> select concat(space(3),'mysql') ;
Mathematical functions
1, BIN (decimal number): Convert decimal to binary
2, CEILING(number) Round up mysql> select ceiling(10.12); Result: 11
3、FLOOR(number) 向下取整 mysql> select ceiling(10.12); 结果:10
4、MAX(column) 获取 最大列
5、MIN(column) 获取最小列
6、SQRT(num) 开平方
7、RAND() 返回0-1之间的随机数值
日期函数
1、CURDATE() 返回当前日期格式 yyyy-MM-dd
2、CURTIME()返回档期时间 12:11:56
3、NOW()返回当前时间 2017-05-12 21:12:34
4、UNIX_TIMESTAMP(date) 返回当前date的时间戳
5、FROM_UNIXTIME()返回UNIX时间戳的日期值
6、WEEK(date)返回当前时间date为一年中的第几周
7、YEAR(data)返回当前时间date的年份
8、DATEDIFF(expr1,expr2) 返回expr1与expr2之间的天数
五、预处理语句
无变量:
创建预处理语句:mysql> prepare stmt1 from 'select * from hello where id>5';创建一个名为stmt1的预处理语句
执行预处理语句:mysql> execute stmt1;执行stmt1预处理语句
带变量:
创建带参数的预处理语句:mysql> prepare stmt1 from 'select * from hello where id>?'
设置变量:mysql> set @i=6;
执行预处理语句:mysql> execute stmt2 using @i;
删除预处理语句:mysql> drop prepare stmt2; #mysql> DEALLOCATE PREPARE stmt2;
注意:每一次执行完EXECUTE时,养成好习惯,须执行DEALLOCATE PREPARE … 语句,这样可以释放执行中使用的所有数据库资源(如游标)。
不仅如此,如果一个session的预处理语句过多,可能会达到max_prepared_stmt_count的上限值。
六、事务处理
mysql默认事务是自动提交的。在做mysql事务处理时请将数据库或者表的ENGINE 设置为InnoDB
将表的存储引擎设置为INNODB:mysql> alter table hello engine=innodb;
设置mysql为非自动提交:mysql> set autocommit=0;
产生事务:mysql> delete from hello where id>7;
事务回滚:mysql> rollback;
事务提交:mysql> commit;
关于事务中的还原点:
创建一个事务:mysql> insert into hello (sex,degree,name) values(1,12312.32,'HHH');
对该事务设置还原点:mysql> savepoint p1;
回滚到指定的还原点:mysql> rollback to p1; 此时事务恢复到p1,也就是p1之后的事务p2 ,p3..这些还原点将失效。
回滚到原始的还原点:mysql> rollback;
七、存储过程
<!-- 创建存储过程 hello1()--> CREATE PROCEDURE hello1() BEGIN SET @i=0; WHILE @i<100 DO insert INTO hello (sex,degree,name) VALUES(1,@i,CONCAT('name',@i)); SET @i=@i+1; END WHILE; end; <!-- 查看存储--> SHOW PROCEDURE STATUS; <!-- 查看hello1()存储过程--> show CREATE PROCEDURE hello1; <!-- 执行存储过程--> CALL hello1;
八、重排auto_increment值
mysql中我们的主键id如果设置为主键自增策略,那我们如何清空表,并且恢复自增列id的值。
方式一:使用truncate table tableName; 该方式在清空表的同时恢复auto_increment 的值。
方式二:
1、mysql> delete from hello3; 清空表 (该方式效率较低)
2、mysql> alter table hello3 auto_increment=1; 恢复auto_increment 的起始值为1
The above is the detailed content of Learn more about the basic operations of Mysql. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings
