mysql数据库学习笔记之常用操作命令_MySQL
bitsCN.com
mysql数据库学习笔记之常用操作命令
1、创建数据库
mysql> create database user;
Query OK, 1 row affected (0.00 sec)
2、使用此数据库
mysql> use user;
Database changed
3、在此数据库上创建表
mysql> create table person(
-> id int unsigned not null auto_increment primary key,
-> name varchar(30)
-> );
Query OK, 0 rows affected (0.00 sec)
4、查看此person表的表结构
mysql> desc person;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(30) | YES | | NULL | |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
5、创建person_bak,并是此表的表结构与person一样,即复制person的表结构
mysql> create table person_bak like person;
Query OK, 0 rows affected (0.01 sec)
6、向person表中插入数据
mysql> insert into person (name) values ("user1");
Query OK, 1 row affected (0.00 sec)
7、将person表中的数据复制到person_bak表中
mysql> insert into person_bak select * from person;
Query OK, 10 rows affected (0.01 sec)
Records: 10 Duplicates: 0 Warnings: 0
8、向person表中创建name列的索引
方法一:
mysql> create index in_name on person (name);
Query OK, 10 rows affected (0.00 sec)
Records: 10 Duplicates: 0 Warnings: 0
方法二:
mysql> alter table person add index in_name (name);
Query OK, 10 rows affected (0.01 sec)
Records: 10 Duplicates: 0 Warnings: 0
9、查看索引
mysql> show index from person;
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| person | 0 | PRIMARY | 1 | id | A | 10 | NULL | NULL | | BTREE | |
| person | 1 | in_name | 1 | name | A | NULL | NULL | NULL | YES | BTREE | |
+--------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
2 rows in set (0.01 sec)
10、在person表中创建唯一索引
mysql> alter table person add unique index un_name (name);
Query OK, 10 rows affected (0.01 sec)
Records: 10 Duplicates: 0 Warnings: 0
11、修改列的属性
mysql> alter table person modify name varchar(20);
Query OK, 10 rows affected (0.01 sec)
Records: 10 Duplicates: 0 Warnings: 0
12、统计表中的数据数据
mysql> select count(*) from person;
+----------+
| count(*) |
+----------+
| 10 |
+----------+
1 row in set (0.00 sec)
13、创建一个视图
mysql> create view v_person as select * from person;
Query OK, 0 rows affected (0.01 sec)
14、查看视图(和查看表的命令一样)
当删除表中的某条记录时,相应的此表对应的视图中的满足条件的记录也将会被删除掉
mysql> show tables;
+----------------+
| Tables_in_user |
+----------------+
| person |
| person_bak |
| v_person |
+----------------+
3 rows in set (0.00 sec)
15、删除视图
mysql> drop view v_person;
Query OK, 0 rows affected (0.00 sec)
16、字符串连接函数---concat("string1","string2") 别名
mysql> select concat("li","haichao") myname;
+-----------+
| myname |
+-----------+
| lihaichao |
+-----------+
1 row in set (0.00 sec)
17、大写转换成小写的函数---lcase(string1)
mysql> select lcase("LHC");
+--------------+
| lcase("LHC") |
+--------------+
| lhc |
+--------------+
1 row in set (0.00 sec)
18、将字符串转换成大写的函数----ucase(string1);
mysql> select ucase("lhc");
+--------------+
| ucase("lhc") |
+--------------+
| LHC |
+--------------+
1 row in set (0.00 sec)
19、判断字符串长度的函数length(string1);
mysql> select length("lhc");
+---------------+
| length("lhc") |
+---------------+
| 3 |
+---------------+
1 row in set (0.02 sec)
20、去除前端和后端的空格函数 ltrim()和rtrim()
21、将指定的字符串重复n次,repeat(string ,count)
mysql> select repeat("linux",3);
+-------------------+
| repeat("linux",3) |
+-------------------+
| linuxlinuxlinux |
+-------------------+
1 row in set (0.02 sec)
22、字符串替换函数
在"linux is very good"中寻找linux,并将其替换成php
mysql> select replace("linux is very good","linux","php");
+---------------------------------------------+
| replace("linux is very good","linux","php") |
+---------------------------------------------+
| php is very good |
+---------------------------------------------+
1 row in set (0.01 sec)
23、字符串截取函数substring("str",int 1,int 2)
在str字符串中从int1开始(从1计)到int2结束(包含),取其字段
mysql> select substring("linux is very good",1,5);
+-------------------------------------+
| substring("linux is very good",1,5) |
+-------------------------------------+
| linux |
+-------------------------------------+
1 row in set (0.00 sec)
24、space()函数:生成空格的函数,通常与concat函数一起使用
mysql> select concat(space(50),"linux");
+---------------------------------------------------------+
| concat(space(50),"linux") |
+---------------------------------------------------------+
| linux |
+---------------------------------------------------------+
1 row in set (0.02 sec)
25、十进制转二进制函数BIN()
mysql> select BIN(255);
+----------+
| BIN(255) |
+----------+
| 11111111 |
+----------+
1 row in set (0.00 sec)
26、向上取整函数CEILING(),比如5.6则为6,向下取整floor(),比如5.6则为5
mysql> select ceiling(5.6);
+--------------+
| ceiling(5.6) |
+--------------+
| 6 |
+--------------+
1 row in set (0.01 sec)
************************************************************************
mysql> select floor(5.6);
+------------+
| floor(5.6) |
+------------+
| 5 |
+------------+
1 row in set (0.00 sec)
27、取最大值和最小值
select sutdent_name,MIN(test_score),MAX(test_score) from student group by student_name;
28、返回随机数:RAND()
mysql> select ceiling( 10*RAND());
+---------------------+
| ceiling( 10*RAND()) |
+---------------------+
| 4 |
+---------------------+
1 row in set (0.00 sec)
bitsCN.com

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, 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.

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

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

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.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting
