Mysql common sql statement (a) - operation of the database

21 test commonly used essential Mysql sql statement, one day knock, knock three times each, monthly cycle, all can remember! !

https://www.cnblogs.com/poloyy/category/1683347.html

 

It should be noted, to create the database and table creation statement Bowen in front oh, the whole family are interrelated Kazakhstan, need to use the database and table created earlier oh

 

Foreword

Called the statement for the operation of the database data definition language (DDL)  

 

What database does it work?

  • Create a database
  • Query the database
  • Modify the database
  • Delete Database
  • Select Database

Referred to as "CRUD", right click directory to jump Oh! -------------- >>>>>>>>>> 

 

Create a database

Syntax

The CREATE  DATABASE  [ the IF the NOT EXISTS ]  < database name > 
[ [the DEFAULT ]  the CHARACTER  the SET  < character set name > ] 
 [ [the DEFAULT ] the COLLATE < collation name > ];

Knowledge Point

  • [] The contents can not fill, have default values
  • MySQL character set (CHARACTER) and collation (COLLATION) are two different concepts.
  • Character set: MySQL storage mode is used to define a string
  • Collation: define the way the comparison string

 

The actual chestnuts

# Create a simple database
 the Create  Database yytest;

# Add conditional, create a database
create database if not exists yytests;

# Specify the database data set, create a database
create database if not  exists yytestss character set utf8;

# Validation rules specified string, create a database
create database if not  exists  yytestsss character set utf8 collate utf8_general_ci;

What # view the database is created statement
show create database yytestsss;

 

Query the database

Syntax

DATABASES SHOW [ the LIKE 'database name' ] ;

Knowledge Point

  •  [ Like '' ]  is optional, for matching the specified database name
  •  [ Like '' ]  can be fuzzy matching, can be accurately matched
  • Database name in single quotes  ' '  surrounded in double quotes may navicat

 

The actual chestnuts

All database queries #
show DATABASES;

# Fuzzy matching
show databases like '%yytest%';

# Exact match
show databases like 'yytests';

 

Modify the database

Syntax

The ALTER  DATABASE  [ database name ] { 
 [ the DEFAULT ]  the CHARACTER  the SET  < character set name >  | 
[ the DEFAULT ] the COLLATE < collation name > }

Knowledge Point

  • Not necessarily write the name of the database, but the database needs are currently in use
  • Support can only modify the character set and character validation rules, if you want to modify the database name directly through the tool rename the database, Mysql statement is not supported modification
  • Modify the database is not common, you can understand

 

The actual chestnuts

# Modify the default database character set
 use yytest;
 the ALTER  Database  Character  the SET utf8;

# Specify the database character set and modify validation rules
alter database yytest  character set utf8 collate utf8_general_ci;

 

Delete Database

Syntax

The DROP  DATABASE  [ the IF EXISTS ]  < database name >

Knowledge Point

  • If you delete the database does not exist, it will report  1008 - of Can ' t drop Database ' yytest ' ; Database doesn ' t exist 
  • It is proposed to delete the database must add  IF EXISTS 

 

The actual chestnuts

# Simple statement to delete database
 drop  Database yytest;

# If there are not deleted
drop database if exists yytests;

 

Select Database

# Select the database
 use yytestss;

Knowledge Point

  • Effect: for a complete database to another jump of [of] the switching operation of the current database
  • When using  the CREATE DATABASE   following statement to create a database, the database does not automatically become the current database, you need to use   USE  to specify the current database

Guess you like

Origin www.cnblogs.com/poloyy/p/12592037.html