搜尋
首頁資料庫mysql教程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
陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
與其他RDBM相比,MySQL如何處理並發?與其他RDBM相比,MySQL如何處理並發?Apr 29, 2025 am 12:44 AM

MySQLhandlesconcurrencyusingamixofrow-levelandtable-levellocking,primarilythroughInnoDB'srow-levellocking.ComparedtootherRDBMS,MySQL'sapproachisefficientformanyusecasesbutmayfacechallengeswithdeadlocksandlacksadvancedfeatureslikePostgreSQL'sSerializa

MySQL與其他關係數據庫相比如何處理交易?MySQL與其他關係數據庫相比如何處理交易?Apr 29, 2025 am 12:37 AM

mySqlHandLestActionSefectefectionalytheinnodbengine,supportingAcidPropertiessimilartopostgresqlesqlandoracle.1)mySqluessRepeTableReadAbereadasTheDefaultIsolationLeleleteLevel,whatcanBeadJustEdToreDtoreDtoreDtoreadCommittedCommittenCommententCommittedForHigh-TrafficsCenarios.2)

MySQL中有哪些數據類型?MySQL中有哪些數據類型?Apr 29, 2025 am 12:28 AM

MySQL的數據類型分為數值、日期和時間、字符串、二進制和空間類型。選擇正確的類型可以優化數據庫性能和數據存儲。

在MySQL中編寫有效的SQL查詢的最佳實踐是什麼?在MySQL中編寫有效的SQL查詢的最佳實踐是什麼?Apr 29, 2025 am 12:24 AM

最佳實踐包括:1)理解數據結構和MySQL處理方式,2)適當索引,3)避免SELECT*,4)使用合適的JOIN類型,5)謹慎使用子查詢,6)使用EXPLAIN分析查詢,7)考慮查詢對服務器資源的影響,8)定期維護數據庫。這些做法能使MySQL查詢不僅快速,還具備可維護性、可擴展性和資源效率。

MySQL與PostgreSQL有何不同?MySQL與PostgreSQL有何不同?Apr 29, 2025 am 12:23 AM

MySQLisbetterforspeedandsimplicity,suitableforwebapplications;PostgreSQLexcelsincomplexdatascenarioswithrobustfeatures.MySQLisidealforquickprojectsandread-heavytasks,whilePostgreSQLispreferredforapplicationsrequiringstrictdataintegrityandadvancedSQLf

MySQL如何處理數據複製?MySQL如何處理數據複製?Apr 28, 2025 am 12:25 AM

MySQL通過異步、半同步和組複製三種模式處理數據複製。 1)異步複製性能高但可能丟失數據。 2)半同步複製提高數據安全性但增加延遲。 3)組複製支持多主複製和故障轉移,適用於高可用性需求。

您如何使用解釋性語句分析查詢性能?您如何使用解釋性語句分析查詢性能?Apr 28, 2025 am 12:24 AM

EXPLAIN語句可用於分析和提升SQL查詢性能。 1.執行EXPLAIN語句查看查詢計劃。 2.分析輸出結果,關注訪問類型、索引使用情況和JOIN順序。 3.根據分析結果,創建或調整索引,優化JOIN操作,避免全表掃描,以提升查詢效率。

您如何備份並還原MySQL數據庫?您如何備份並還原MySQL數據庫?Apr 28, 2025 am 12:23 AM

使用mysqldump進行邏輯備份和MySQLEnterpriseBackup進行熱備份是備份MySQL數據庫的有效方法。 1.使用mysqldump備份數據庫:mysqldump-uroot-pmydatabase>mydatabase_backup.sql。 2.使用MySQLEnterpriseBackup進行熱備份:mysqlbackup--user=root--password=password--backup-dir=/path/to/backupbackup。恢復時,使用相應的命

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具