MySQL-- basic database operations

Basic Operations Command

1, view the database list information

show databases;

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.11 sec)

2, view the data in the database table information

(1)进入数据库
use mysql;
mysql> use mysql;
Database changed

(2)查看数据表

show tables;

mysql> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| engine_cost               |
| event         
......

3, a table structure information (field)

describe user;

(1)其中PRI为主键(不能为空)
定义——确定表中唯一实体对象的标识
特点——唯一性、非空性
(2)其中Extra为约束条件

mysql> describe user;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field                  | Type                              | Null | Key | Default               | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host                   | char(60)                          | NO   | PRI |                       |       |
| User                   | char(30)                          | NO   | PRI |                       |       |
| Select_priv            | enum('N','Y')                     | NO   |     | N                     |       |
......

4. Create a database

create database named;

mysql> create database auth;
Query OK, 1 row affected (0.00 sec)

SQL statements

QL language

●是Structured Query Language的缩写,即结构化查询语言
●是关系型数据库的标准语言
●用于维护管理数据库,如数据查询、数据更新、访问控制、对象管理等功能

SQL classification

●DDL:数据定义语言
●DML:数据操纵语言
●DQL:数据查询语言
●DCL:数据控制语言

典型数据库索引算法---二分查找
定义:以一个数据为参考,比他小的放左边,比他大的放右边。

DDL operations command

DDL statements used to create database objects such as databases, tables, indexes, etc.

1. Use DDL statements create a new library, table

创建数据库: creste databae 数据库名;

mysql> create database auth;
Query OK, 1 row affected (0.00 sec)

创建数据表:create table 表名 (字段定义……);

mysql> create table info(  
    -> ID int(4) not null,
    -> 姓名 varchar(8) not null,
    -> 住址 varchar(10) not null,
    -> 成绩 decimal default 0,  
    -> primary key (ID));
Query OK, 0 rows affected (0.01 sec)

mysql> desc info; 
+--------+---------------+------+-----+---------+-------+
| Field  | Type          | Null | Key | Default | Extra |
+--------+---------------+------+-----+---------+-------+
| ID     | int(4)        | NO   | PRI | NULL    |       |
| 姓名   | varchar(8)    | NO   |     | NULL    |       |
| 住址   | varchar(10)   | NO   |     | NULL    |       |
| 成绩   | decimal(10,0) | YES  |     | 0       |       |
+--------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

2. Use DDL statement to delete database table

删除指定的数据表:drop table [数据库名.]表名
删除指定的数据库: drop database 数据库名

mysql> drop database auth;
Query OK, 0 rows affected (0.05 sec)

DML operation command

DML statements are used to manage the data in the table
include the following operations.
● INSERT: insert new data
● update: update the original data
● delete: delete unneeded data

1. Insert a new data record to the data table

insert into 表名(字段1,字段2, .....) values(字段1的值,字段的值, .....);

mysql> insert into info values (1,'周妹儿','南京',80);  
Query OK, 1 row affected (0.00 sec)

mysql> insert into info values (2,'张倩娣','南京',66);
Query OK, 1 row affected (0.00 sec)

mysql> insert into info values (3,'李向阳','上海',default);
Query OK, 1 row affected (0.02 sec)

2. modify, update the data table records data PF

update 表名 set 字段名1=值1[,字段名2=值2] where 条件表达式;

mysql> update info set 住址='南京'where ID=3;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1  Changed: 1  Warnings: 0

3. Remove the specified data record in the data table

delete from 表名 where 条件表达式;
不带where条件的语句表示删除表中所有记录(谨慎操作);

#删除表中指定数据记录
mysql> delete from info where ID=2; 
Query OK, 1 row affected (0.02 sec)

#删除表
mysql> drop table info;    
Query OK, 0 rows affected (0.00 sec)

#删除库
mysql> drop database test;  
Query OK, 0 rows affected (0.00 sec)

DQL operation command

DQL is a data query, only one: SELECT
used to find qualified data from the data table records

1. query may specify conditions

selext 字段名1,字段名2..... from 表名;

mysql> select * from info;
+----+-----------+--------+--------+
| ID | 姓名      | 住址   | 成绩   |
+----+-----------+--------+--------+
|  1 | 周妹儿    | 南京   |     80 |
|  3 | 李向阳    | 南京   |      0 |
+----+-----------+--------+--------+
2 rows in set (0.00 sec)

2. The conditions specified query

select 字段名1,字段名2.... from 表名 where 条件表达式;

mysql> select 住址 from info where 住址='南京';
+--------+
| 住址   |
+--------+
| 南京   |
| 南京   |
+--------+
2 rows in set (0.04 sec)

DCL operation command

1. Set user permissions (the user does not exist, the new user)

GRANT  权限列表   ON  数据库名.表名  TO  用户名@来源地址  [IDENTIFIED BY  '密码']

mysql>  grant all privileges on *.* to 'root'@'%' identified by 'abc123';
Query OK, 0 rows affected, 1 warning (0.08 sec)

2. Check the user's permissions

SHOW GRANTS FOR 用户名@来源地址

mysql> show grants for 'root'@'%';
+-------------------------------------------------------------+
| Grants for root@%                                           |
+-------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION |
+-------------------------------------------------------------+
1 row in set (0.00 sec)

3. revoke a user's privileges

REVOKE 权限列表  ON  数据库名.表名   FROM  用户名@来源地址

mysql> revoke all on *.* from 'root'@'%';
Query OK, 0 rows affected (0.00 sec)

Guess you like

Origin blog.51cto.com/14449528/2456519