Basic usage of MySQL query statement

Source: Internet
Author: User

One: MySQL Basic Statement

1. Show available databases

show databases;

2. Create a library database

Create databaselibrary;

3. Using the Database

Use library;

4. View the table

Show tables;

5. Build a Table statement

Mysql> uselibrary;

Database changed

Mysql> createtable Viedos (

-video_id Int (one) not nullauto_increment,

-Title varchar (255) NOT NULL,

Director varchar (255) NOT NULL,

Primary KEY (video_id));

Query OK, 0 rowsaffected (0.30 sec)

6. Inserting data

Insert Intoviedos (Video_id,title,director) values

(1, ' Greater Shanghai ', ' action movies '), (2, ' Little Shanghai ', ' Love Movie '), (3, ' Red Shanghai ', ' comedy '), (4, ' Black Shanghai ', ' gun Shot '), (5, ' Big Shanghai ', ' sanda film ');

7. Delete a table

Delete from table_name;

8. Updating table data

Update Viedos settitle= ' on the beach ' where video_id=1;

Second: Execute the query statement

A: Basic statement

1. Search All

SELECT * FROMVIEDOS;

2. Query section

SELECT title Fromviedos;

3. Remove the same part of the query result

SELECT distinct title from Viedos;

SELECT title from Viedoswhere director= ' action movies ';

B: Using logical comparison operators

Create a new table:

CREATE TABLE Core (

Sname VARCHAR (+) not NULL,

Math INT not NULL,

Physics INT Notnull,

Literature INT Notnull)

Inserting data

INSERT into Corevalues (' John ', 68,37,45), (' Jim ', 96,89,92),

(' Bill ', 65,12,45), (' Harry ', 68,25,82);

Query 1:

SELECT * from Corewhere math>90;

Query 2:

SELECT sname Fromcore WHERE math>85;

Query 3:

SELECT * from Corewhere math<=25 or physics<=25 or literature<=25;

Query 4:

Selectsname,math+physics+literature from Core;

C: Using function queries

Query 1:

SELECT COUNT (*) from core;

Query 2:

SELECT AVG (math) from core;

Query 3:

Selectmin (math), MAX (physics) from core;

D: Filter Query Results

Query 1: Descending

SELECT Math Fromcore ORDER by math DESC;

Query 2: Show two records

SELECT Math Fromcore LIMIT 2, 2;

Query 3: Federated use

SELECT Math Fromcore ORDER by math DESC LIMIT 2, 2;

E: Using wildcard characters to query

Query 1:

SELECT sname fromcore WHERE sname like '%j% ';

Query 2:

SELECT Math fromcore WHERE math like '%6% ';

Copyright Notice: Bo Master original articles, reproduced please indicate the source. Http://blog.csdn.net/dzy21

Basic usage of MySQL query statement

Related Article

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.