mysql常用命令小结_MySQL
一、数据库级操作
1、登陆mysql
常用的格式是 mysql -u user_name -p 按下回车后输入密码再次回车即可进入。
2、查看当前已经存在的所有数据库
show databases; (注意,分号不可省略。少数命令可以忽略分号,但大部分的mysql命令都必须以分号结尾)
3、选择数据库
知道所有数据库名称后,需要选择某个具体的数据库进行操作,其命令为:
use database_name; (注意,这个命令是少数可以忽略分号的命令之一)
e.g. use students;
4、创建数据库
create database database_name; (这里可能需要root权限)
e.g. create database one; 这将创建名为one的数据库。
5、删除数据库
drop database database_name;
二、表级操作
1、查看当前数据库的所有表名
show tables;
2、查看某个表中的所有列名(属性信息)
describe table_name;(也可以简写为:desc table_name;)
show columns from table_name;
show create table table_name;
3、查看表中的某一列
select column_1 from table_name;
若想查看某几列则需要用逗号隔开列名,如:select column_1, column_2 from table_name;
也可以使用通配符* 查看所有列,如:select * from table_name;
4、创建表
create table table_name (column_name1 type constraint, column_name2 type constraint, ..., column_nameX type constraint, primary key (column_name));
这里的constraint包括not null(非空)、unique(不能重复)等,是选填的,即可以没有;
type包括char(n) varchar(n) int numeric float real double precision;(注意,mysql的字符串要用单引号括起来)
最后面的主键约束是必须要有的,primary key()括号里面的属性可以是一个,也可以是逗号隔开的几个。
5、删除表
drop table table_name; (这将彻底删除此表,即show tables;命令不会再显示它)
(注意与之相近的一个命令:delete from table_name; 这个命令只是清空该表,但是表模式仍然存在,即show tables;命令仍然显示它)
6、向表中添加/删除列名(属性名)
alter table table_name add column_name type;
alter table table_name drop column_name;
7、修改表中的某个列
alter table table_name change old_col new_col type;
三、行级操作
1、向表中添加行(记录,record)
insert into table_name values(....); (注意,括号里面的值必须要对应创建表时列的前后顺序,且用逗号隔开)
如果忘了列名的顺序且嫌查看顺序麻烦,可以用这种形式:
insert into table table_name(col_1, col_2, ... col_n) values (val_1, val_2, ... val_n);
这时只要val_n对应col_n就行了,而不用去管列名的真正顺序。
2、删除表中特定的行
delete from table_name where P;
P是一个条件,一般格式是:col_name = value,即属性等于某个值的一行。
3、修改/更新行
update table_name set col_name = new_val where P;
四、用户权限操作(一般需要root权限)
1、查看所有存在用户
select user from mysql.user;
2、查看当前用户
select user();
3、创建一个用户
create user user_name identified by 'passwd';
这就创建了一个名为user_name的用户,其可以使用密码passwd登陆mysql,但是其权限仅限于登陆而已,登陆之后什么也做不了。这就需要在赋予其相关权限之后才能进行某些操作。
如果没有后面的 identified by 'passwd',将会创建一个无密码的用户,登陆是只需输入 mysql -u user_name回车即可,不必再输入密码。
上面这条命令的效果和下面这条是一样的(前提是用root登陆):
insert into mysql.user(Host,User,Password) values('localhost','user_name',password('passwd'));
他们本质上都是修改了mysql的元数据库mysql中的表user,这个表记录着mysql的所有用户信息。注意到,第二条命令多了一个参数Host,这在第一条命令中是被默认的,但是在第二条中是必不可少的,否则将无法登陆本机的mysql。
当然Host的值是可以改动的,对于第二条命令,其改动是显然的;对于第一条命令,如果不想选择默认值'localhost',可以这样:
create user_name@other_host identified by 'passwd';
4、赋予用户相关权限
权限的范围包括经典的“增删改除”,以及代表所有的all等。整体格式如下:
grant privilege_list on database_name.table_name to user_name@'host_name';
privilege_list可以是select/update/delete/insert中的一个或几个(逗号隔开),或者直接用all代替,代表所有权限;
on则将权限限定在某个数据库的某个表上,这里可以使用通配符*代表所有,如database_one.* 就意味着在database_one的所有表上都有权限,当然database_name也可以用*替代 ;
to后面跟的是user_name, 后面的@'host_name'可以省略,默认值为localhost,也可以使用通配符%来代表所有主机。需要注意的是,如果user_name本来不存在的话,这个命令将创建一个新的用户,其名称就是user_name, 这就是另一种创建用户的方法,而且同时赋予了相关权限,我本人一般使用这种方法。需要密码的话,可以在后面添上 identified by 'passwd',否则就是无密码用户。

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

AI Hentai Generator
Generate AI Hentai for free.

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



The sudo command allows users to run commands in elevated privilege mode without switching to superuser mode. This article will introduce how to simulate functions similar to sudo commands in Windows systems. What is the Shudao Command? Sudo (short for "superuser do") is a command-line tool that allows users of Unix-based operating systems such as Linux and MacOS to execute commands with elevated privileges typically held by administrators. Running SUDO commands in Windows 11/10 However, with the launch of the latest Windows 11 Insider preview version, Windows users can now experience this feature. This new feature enables users to

This article will introduce readers to how to use the command prompt (CommandPrompt) to find the physical address (MAC address) of the network adapter in Win11 system. A MAC address is a unique identifier for a network interface card (NIC), which plays an important role in network communications. Through the command prompt, users can easily obtain the MAC address information of all network adapters on the current computer, which is very helpful for network troubleshooting, configuring network settings and other tasks. Method 1: Use "Command Prompt" 1. Press the [Win+X] key combination, or [right-click] click the [Windows logo] on the taskbar, and in the menu item that opens, select [Run]; 2. Run the window , enter the [cmd] command, and then

1. Overview The sar command displays system usage reports through data collected from system activities. These reports are made up of different sections, each containing the type of data and when the data was collected. The default mode of the sar command displays the CPU usage at different time increments for various resources accessing the CPU (such as users, systems, I/O schedulers, etc.). Additionally, it displays the percentage of idle CPU for a given time period. The average value for each data point is listed at the bottom of the report. sar reports collected data every 10 minutes by default, but you can use various options to filter and adjust these reports. Similar to the uptime command, the sar command can also help you monitor the CPU load. Through sar, you can understand the occurrence of excessive load

In Win11 system, you can enable or disable Hyper-V enhanced session mode through commands. This article will introduce how to use commands to operate and help users better manage and control Hyper-V functions in the system. Hyper-V is a virtualization technology provided by Microsoft. It is built into Windows Server and Windows 10 and 11 (except Home Edition), allowing users to run virtual operating systems in Windows systems. Although virtual machines are isolated from the host operating system, they can still use the host's resources, such as sound cards and storage devices, through settings. One of the key settings is to enable Enhanced Session Mode. Enhanced session mode is Hyper

What is the correct way to restart a service in Linux? When using a Linux system, we often encounter situations where we need to restart a certain service, but sometimes we may encounter some problems when restarting the service, such as the service not actually stopping or starting. Therefore, it is very important to master the correct way to restart services. In Linux, you can usually use the systemctl command to manage system services. The systemctl command is part of the systemd system manager

LSOF (ListOpenFiles) is a command line tool mainly used to monitor system resources similar to Linux/Unix operating systems. Through the LSOF command, users can get detailed information about the active files in the system and the processes that are accessing these files. LSOF can help users identify the processes currently occupying file resources, thereby better managing system resources and troubleshooting possible problems. LSOF is powerful and flexible, and can help system administrators quickly locate file-related problems, such as file leaks, unclosed file descriptors, etc. Via LSOF Command The LSOF command line tool allows system administrators and developers to: Determine which processes are currently using a specific file or port, in the event of a port conflict

Linux is a powerful operating system that provides many efficient inter-process communication mechanisms, such as pipes, signals, message queues, shared memory, etc. But is there a simpler, more flexible, and more efficient way to communicate? The answer is yes, that is eventfd. eventfd is a system call introduced in Linux version 2.6. It can be used to implement event notification, that is, to deliver events through a file descriptor. eventfd contains a 64-bit unsigned integer counter maintained by the kernel. The process can read/change the counter value by reading/writing this file descriptor to achieve inter-process communication. What are the advantages of eventfd? It has the following features

Detailed explanation of the Linuxldconfig command 1. Overview In the Linux system, ldconfig is a command used to configure shared libraries. It is used to update the links and cache of shared libraries and enable the system to load dynamically linked shared libraries correctly. The main function of ldconfig is to find dynamic link libraries and create symbolic links for program use. This article will delve into the usage and working principle of the ldconfig command, and use specific code examples to help readers better understand the functions of ldconfig
