Common basic MySQL operations

Source: Internet
Author: User

# Common basic operations for Structured Query Language in MySQL:
/* DDL (Data Definition Language) Data Definition Language */
# Create a database:
Create database 'database name' charset utf8;
# Delete a database:
Drop database 'database name ';
# Display all databases:
Show databases;
# Use a database
Use 'database name ';
# Determine the currently used database:
Select database ();
# Display the structure of a table in the database
Desc 'table name ';
# Display the statement for creating a table
Show create table 'table name ';
# Create a table:
Create table 'table name '(
'Column name' 'column description ',
'Column name' 'column description ',
'Column name' 'column description ');
# Tables with primary keys and auto-Increment
Create table 'table name '(
'Column name' 'column description' 'Primary key auto_increment,
'Column name' 'column description ',
'Column name' 'column description ');
# Delete a table:
Drop table 'table name ';
# Modify a table:
Alter table 'old table name' rename'new table name ';
# Add fields
Alter table 'table name' add column 'column name' 'column description ';
# Modifying Fields
Alter table 'table name' change 'old column name' new column description ';
# Delete a field
Alter table 'table name'drop column' column name ';
/* DML (Data Manipulation Language) Data operation Language */
# Data Entry
Insert into 'table name' ('field name, field name... ') values ('corresponding value, corresponding value ...');
Insert into 'table name' values ('corresponding value, corresponding value ...');
# Update data
Update 'table name 'set' field name' = 'field value', 'field name' = 'field value'... where 'field name' = 'field value ';
Update 'table name 'set' field name' = 'field value', 'field name' = 'field value '...;
# Delete data
Delete from 'table name ';
Delete from 'table name' where 'field name' = 'field value ';
/* DQL (Data Queries Language) Data Query Language */
# Query all
Select * from 'table name ';
# Query required
Select 'field name', 'field name'... from 'table name ';
# Alias Query
Select 'field name', concat ('field name', 'field name') [as] 'Alias 'from 'table name ';
# Where Query
Select * from 'table name' where 'field name' like "_' value '%"
# Aggregate Query
Select count (*) from 'table name'; # Number of query records
Select 'field name' from 'table name' order by 'field name' desc; # query in descending order
Select distinct 'field name' from 'table name' order by 'field name' asc; # duplicate query in ascending order
# Query groups
Select avg ('field name') from 'table name' group by 'field name ';
Select avg (field name) as 'Alias ', 'Alias' from 'field name 'group by' field name 'having' field name '> 0;
/* DCL (Data Control Language) Data Control Language */
/* Constraint */
# Primary key constraints
Alter table 'table name' add constraint primary key ('field name ');
# Unique constraints
Alter table 'table name' add constraint unique ('field name ');
# Foreign key constraints
Alter table 'table name' add constraint foreign key ('foreign key field name') references 'Primary table' ('Primary key field name ');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.