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

Some ways to eliminate duplicate rows in MySQL

SQL statement /* Some methods of eliminating dupl...

A brief discussion on whether MySQL can have a function similar to Oracle's nvl

Use ifnull instead of isnull isnull is used to de...

Bundling non-JavaScript static resources details

Table of contents 1. Custom import in packaging t...

Some small methods commonly used in html pages

Add in the <Head> tag <meta http-equiv=&q...

MySQL 5.6.36 Windows x64 version installation tutorial detailed

1. Target environment Windows 7 64-bit 2. Materia...

Sublime Text - Recommended method for setting browser shortcut keys

It is common to view code effects in different br...

How to enable remote access in Docker

Docker daemon socket The Docker daemon can listen...

Solution to Chinese garbled characters when operating MySQL database in CMD

I searched on Baidu. . Some people say to use the...

Docker volumes file mapping method

background When working on the blockchain log mod...

HTML is the central foundation for the development of WEB standards

HTML-centric front-end development is almost what ...

jQuery manipulates cookies

Copy code The code is as follows: jQuery.cookie =...

Theory Popularization——User Experience

1. Concept Analysis 1: UE User Experience <br ...