Home  >  Article  >  Database  >  Summary of commonly used basic SQL statements in MySQL_MySQL

Summary of commonly used basic SQL statements in MySQL_MySQL

WBOY
WBOYOriginal
2016-09-09 08:13:39941browse

1. Common commands

Connect local database and remote database (172.16.xx.xx:3306):

mysql -h localhost -u root -p123 
mysql -h 172.16.xx.xx -P 3306 -u root -p 

2. DDL

Data Definition Language (DDL) defines the database schema, including CREATE, ALTER, DROP, TRUNCATE, COMMENT and RENAME statements.

CREATE

The create statement creates a table:

CREATE TABLE `device_label` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键-自增长ID',
`origin_model` varchar(64) COLLATE utf8_bin NOT NULL COMMENT '收集机型',
`origin_vendor` varchar(64) COLLATE utf8_bin NOT NULL COMMENT '收集厂商',
`vendor` varchar(32) COLLATE utf8_bin NOT NULL COMMENT '标注厂商',
`model` varchar(32) COLLATE utf8_bin NOT NULL COMMENT '标注品牌',
PRIMARY KEY (`id`),
UNIQUE KEY `device_key` (`origin_model`,`origin_vendor`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='设备标注表'; 
改(ALTER)

ALTER TABLE changes the structure of the table and supports the following operations,

Modify column type and column name:

alter table device_label modify origin_model varchar(32);
alter table device_label change origin_model device_model varchar(16); 

Append column:

alter table device_label add os_type varchar(8) COLLATE utf8_bin NOT NULL COMMENT '操作系统' after id; 

Modify the order between columns:

alter table device_label modify os_type varchar(8) after origin_model; 

Modify primary key:

alter table device_label drop primary key, add primary key (`origin_model`,`origin_vendor`); 

TRUNCATE

TRUNCATE clears the table, which is equivalent to delete from without specifying the where condition.

truncate device_label;

3. DCL

Data Control Language (DCL) is used for user permission management, including GRANT and REVOKE commands.

GRANT

MySQL has very fine permission control:

•Detailed permission classification
•DB -> Table -> Column, granularity of permissions
•Control the host (wildcard matching available)

Create a hive user and grant localhost permission to access all tables in db1:

CREATE USER 'hive'@'localhost' IDENTIFIED BY 'mypass';
GRANT ALL ON db1.* TO 'hive'@'localhost';
-- 可简写为
GRANT ALL ON db1.* TO 'hive'@'localhost' IDENTIFIED BY 'mypass'; 

You can also only grant select permission to a certain table:

GRANT SELECT ON db2.invoice TO 'hive'@'localhost'; 

4. DML

Data manipulation language (DML) is mainly used to express database queries and updates, including additions, deletions, modifications (INSERT, UPDATE, DELETE, SELECT).

INSERT

Add row data:

insert into device_label (origin_model, origin_vendor, vendor, model)
values (
'h9', 'bbk', '步步高', 'H9'
); 

Copy one table to another table:

insert into device_label_copy (`origin_model`, `origin_vendor`, `vendor`, `model`)
select `origin_model`, `origin_vendor`, `vendor`, `model`
from device_label; 

In addition, MySQL supports loading structured plain text into the database via load data:

load data local infile 'dvc-label.csv'
into table device_label
fields terminated by ','
ignore 1 lines
(origin_model, origin_vendor, vendor, model); 

If ERROR 1148 (42000) error occurs, use the mysql --local-infile -u user -ppasswd command to enter mysql.

Update

Update column value:

update device_label 
set origin_model = 't2', origin_vendor = 'xiami'
where vendor = '锤子'; 

DELETE

Delete rows based on where condition:

delete from device_label where origin_vendor = 'alps'; 

Check (SELECT)

Query manufacturers with more than 10 marked models:

select vendor, count(distinct model) as models
from device_label
group by vendor
having models > 10

The above is a summary of the commonly used basic SQL statements in MySQL introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support of the 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