Summary of basic SQL statements in MySQL database

Summary of basic SQL statements in MySQL database

This article uses examples to describe the basic SQL statements of the Mysql database. Share with you for your reference, the details are as follows:

Basic SQL Statements

1. Login, logout and shortcut keys:

(1) Shortcut keys:

————Quickly return to the beginning of the line
ctrl + a

————Back to the end of the line
ctrl + e

————Clear screen
ctrl + l

----Finish
ctrl + c + space

(2) Connect to the database:

————Show password

mysql -uroot -pmysql

————Do not show password

mysql -uroot -p 
Enter password

(3) Exit the database:

quit/exit/ctrl + d

(4) Some commands:

————Show database version

select version();

————Display time

select now();

2. Database operations:

(1) Create:

————Create a database

create database database name charset=utf8;

(2) Usage:

————Using the database

use database name

(3) View:

————View the currently used database

select database();

————View all databases

show databases;

————View the statement to create a database

show create database database name;

(4) Delete:

————Delete the database

drop database database name;

(5) Database backup and recovery (understanding):

————Backup

mysqldump -uroot -p database name> xxx.sql

----recover

mysql -uroot -p new database name < xxx.sql

3. Table structure operation:

(1) View:

————View all tables in the current database

show tables;

————View the table structure

desc table name

————View the table creation statement

show create table table name;

(2) Create:

————Create table

create table table name( 
Field name data type constraints, 
…….. 
…….. 
); 
(auto_increment: automatic growth)

(3) Modifications:

————Add fields

alter table table name add field name type constraint;

————Modify fields (not rename)

alter table table name modify field name type constraint;

————Modify field (rename)

alter table table name change old field name new field name type constraint;

————Delete field

alter table table name drop field name;

(4) Delete:

————Delete table:

drop table table name;

4. Table data operation:

(1) View:

————View all fields

select * from table name;

————View the specified field

select field name, field name from table name;

————View by conditions

select * from table name where condition;

————Give the field an alias to view

select field name as alias from table name;

(2) Addition:

————Full column insert

insert into table name values ​​(record);

————Partial insertion

insert into table name (field name, field name) values ​​(record);

————Multiple row inserts

insert into table name values ​​(record 1), (record 2)…;

(3) Delete:

————Physical deletion

delete from table name where condition;

————Logical deletion

alter table table name add delete information field name bit dafault 0; 
update table name set delete information field name = 1 where condition;

(4) Modifications:

————All changes

update table name set modification information;

————Modify according to conditions

update table name set modify information where condition;

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL common functions summary", "MySQL log operation skills", "MySQL transaction operation skills summary", "MySQL stored procedure skills" and "MySQL database lock related skills summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • MySQL select, insert, update batch operation statement code examples
  • Python MySQLdb parameter passing method when executing sql statements
  • Detailed summary of mysql sql statements to create tables
  • MySQL fuzzy query statement collection
  • Analysis of the usage of loop statements (WHILE, REPEAT and LOOP) in MySQL stored procedures
  • Advanced and summary of commonly used sql statements in MySQL database
  • Simply understand the writing and execution order of MySQL statements

<<:  Solve the problem that the docker container cannot ping the external network

>>:  Implementing a simple calculator with javascript

Recommend

Detailed explanation of the correct use of the count function in MySQL

1. Description In MySQL, when we need to get the ...

Memcached method for building cache server

Preface Many web applications store data in a rel...

JQuery implements hiding and displaying animation effects

This article shares the specific code of JQuery t...

Echarts tutorial on how to implement tree charts

Treemaps are mainly used to visualize tree-like d...

How to connect to docker server using ssh

When I first came into contact with docker, I was...

How to configure two-way certificate verification on nginx proxy server

Generate a certificate chain Use the script to ge...

Solve the problem of blank gap at the bottom of Img picture

When working on a recent project, I found that th...

MySQL replication mechanism principle explanation

Background Replication is a complete copy of data...

Detailed tutorial on installing and configuring MySQL 5.7.20 under Centos7

1. Download the MySQL 5.7 installation package fr...

Detailed explanation of two points to note in vue3: setup

Table of contents In vue2 In vue3 Notes on setup ...

A brief analysis of how MySQL implements transaction isolation

Table of contents 1. Introduction 2. RC and RR is...

Install redis and MySQL on CentOS

1|0MySQL (MariaDB) 1|11. Description MariaDB data...

MySQL Learning: Three Paradigms for Beginners

Table of contents 1. Paradigm foundation 1.1 The ...

Windows 10 1903 error 0xc0000135 solution [recommended]

Windows 10 1903 is the latest version of the Wind...