2. Basic Use and Management of MySQL databases; 2. mysql Database Management

Source: Internet
Author: User

2. Basic Use and Management of MySQL databases; 2. mysql Database Management
1. mysql Data Storage Structure

1.1 composed of "Database" -- "table" -- "data"

1.2 SQL is required for database management (Structured Query Language)

The SQL language is divided:

1. Data Query Language DQL

Searches existing database data by specified combination, condition expression, or sort without changing the database data.

Command: SELECT... FROM... WHERE...

2. Data manipulation language DML

Inserts, deletes, and modifies existing databases.

Command: INSERT, UPDATE, DELETE

3. Data Definition Language DDL

Creates, modifies, or deletes various objects in the database, including tables, views, and indexes.

Command: create table, create view, create index, alter table,
Drop table, drop view, DROP INDEX

4. Data Control Language DCL

It is used to grant or revoke some privileges to access the database, control the time and Effect of data operation transactions, and monitor the database.

Command: GRANT, REVOKE, COMMIT, ROLLBACK

2. query all databases

Log on to the database through the command line and enter the first SQL statement to view the database

Mysql> show databases; -- four databases are displayed by default.

+ -------------------- +
| Database |
+ -------------------- +
| Information_schema | -- mysql metadata, basic data
| Mysql | -- mysql configuration database, which contains user information (user name and password, permission management)
| Performance_schema | -- mysql database software running data, log information, and performance data
| Test | -- test Database, empty Database
+ -------------------- +

3. Create a database

3.1 The SQL statement is: create database name;

Mysql> create database first; -- first is the database name

Query OK, 1 row affected (0.01 sec)

    3.2 set database characters when creating a database

Mysql> create database two -- you do not need to enter a semicolon at this time, because the semicolon indicates that the statement is over and press Enter.

-> Default character set utf8; -- set the default character to UTF-8.
Query OK, 1 row affected (0.00 sec)

  3.3 view the default Character Set of the database

    Mysql> show create database first; -- first is the database name

+ ---------- + --------------------------------------------------------------- +
| Database | Create Database |
+ ---------- + --------------------------------------------------------------- +
| First | create database 'first '/*! 40100 DEFAULT CHARACTER SETGbk*/|
+ ---------- + --------------------------------------------------------------- +
1 row in set (0.00 sec)

    3.4 delete a database

Mysql> drop database two; -- two is the database name

Query OK, 0 rows affected (0.17 sec)

3.5 modify the default Character Set of the database

Mysql> alter database first default character set utf8; -- modify the character of the first database to UTF-8

Query OK, 1 row affected (0.00 sec)

4. Table Management

4.1 select database

Mysql> use first; -- first indicates the database name.
Database changed

4.2 create a table

Mysql> create table student (-- student is the table name
-> Sid int, -- sid is the field name number, and int Is the field type Integer
-> Sname varchar (20), -- sname is the field name, And varchar (20) is the field type. the string length is 20.
-> Sage int -- sage indicates the age of the field name, int indicates the field type, and integer indicates the age of the field name.
-> );
Query OK, 0 rows affected (0.14 sec)

4.3 view all tables

    Mysql> show tables;

+ ----------------- +
| Tables_in_first |
+ ----------------- +
| Student |
+ ----------------- +
1 row in set (0.00 sec)

     4.4 view the table structure of a table

Mysql> desc student; -- student table name

+ ------- + ------------- + ------ + ----- + --------- + ------- +
| Field | Type | Null | Key | Default | Extra |
+ ------- + ------------- + ------ + ----- + --------- + ------- +
| Sid | int (11) | YES | NULL |
| Sname | varchar (20) | YES | NULL |
| Sage | int (11) | YES | NULL |
+ ------- + ------------- + ------ + ----- + --------- + ------- +
3 rows in set (0.05 sec)

4.5 delete a table

Mysql> drop table student;

Query OK, 0 rows affected (0.11 sec)

4.6 modify a table

Mysql> alter table student add column sgender varchar (2); -- add an sgender field to the student table. column can be omitted.

Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0

    

Mysql> alter table student drop sgender; -- delete the sgender field in the student table. column can be omitted.

Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0

      

Mysql> alter table student modify sname varchar (10); -- modify the sname field type in the student table to varchar (10)
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0

      

Mysql> alter table student change sname newsname varchar (20); -- modify the name of the sname field in the student table to newsname
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0

 

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.