Home > Database > Mysql Tutorial > body text

Summary of commonly used basic SQL statements in MySQL_MySQL

WBOY
Release: 2016-09-09 08:13:39
Original
971 people have browsed it

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 
Copy after login

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)
Copy after login

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); 
Copy after login

Append column:

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

Modify the order between columns:

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

Modify primary key:

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

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'; 
Copy after login

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

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

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 after login

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; 
Copy after login

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); 
Copy after login

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 = '锤子'; 
Copy after login

DELETE

Delete rows based on where condition:

delete from device_label where origin_vendor = 'alps'; 
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template