mysql sql statement

(A) mysql client command

management:

\h help :   查看帮助
\G:格式化查询
\s status:查看数据库的状态信息
\ source:导入sql文件
\u use:切换数据库
\! system:在数据库中使用系统命令

(B) mysqladmin Client Management

#查看MySQL进程是否存活 
[root@db01 ~]# mysqladmin ping mysqld is alive 
#查看mysql信息 
[root@db01 ~]# mysqladmin status 
#关闭mysql进程 
[root@db01 ~]# mysqladmin shutdown 
#查看MySQL当前参数
[root@db01 ~]# mysqladmin variables 
#库外创建数据库
[root@db01 ~]# mysqladmin create aaa 
#库外删除数据库 
[root@db01 ~]# mysqladmin drop aaa 
#刷新binlog日志
[root@db01 ~]# mysqladmin flush-log 
#修改密码 
[root@db01 ~]# mysqladmin password '123'

(C) sql statement

1.DDL data definition language

Development specification: library name, table name must be lowercase, or else will accidentally deleted data

1. Library

create (create)

View command creates

mysql> help create user;
语法:
Syntax: CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [create_specification] ...
#为了避免库已存在 报错
mysql> create database if not exists zhp;
Query OK, 1 row affected, 1 warning (0.00 sec)
#规范创建数据库
mysql> create database if not exists zhp default character set utf8 default collate utf8_general_ci;

drop (delete)

mysql> drop database bb;

alter (modify)

Modify the character set

mysql> alter database zhp charset gbk;
Query OK, 1 row affected (0.00 sec)
查看
mysql> show create database zhp;  
+----------+-------------------------------------------------------------+
| Database | Create Database                                             |
+----------+-------------------------------------------------------------+
| zhp      | CREATE DATABASE `zhp` /*!40100 DEFAULT CHARACTER SET gbk */ |
+----------+-------------------------------------------------------------+
1 row in set (0.00 sec)

Modify validation rules

mysql> alter database zhp collate utf8_bin;

1572867737952

2. Table

Create a table create

View table creation statement help

mysql> create table tlbb(
    -> aid int primary key auto_increment comment '学号',
    -> sname varchar(10) not null comment'玩家名字',
    -> sage tinyint unsigned comment '玩家等级',
    -> sgender enum('m','f')not null default 'm' comment '玩家性别', 
    -> cometime datetime not null default NOW()comment '时间');
Query OK, 0 rows affected (0.04 sec)

drop (drop table)

mysql> drop table tlbb;

ALTER (table modified)

Modify the table name

mysql> alter table tlbb rename student;

Increase the field

mysql> alter table stu add gsb varchar(10);

The top field into

mysql> alter table stu add youfeng int first;

View

mysql> desc student2;

(D) data types

int:整数 -2^31-2^31-1
varchar:字符类型(变长)
char:字符类型(定长)
tinyint:最小整数-128—-128
enum:枚举类型   
datetime:时间类型 年月日时分秒

constraint

not null:非空
primary key:主键(唯一,且非空)
unique key:唯一键(可与为空)
auto_increment:自增
unsigned:无符号,和数字结合,就是非负数
default:默认值
commennt:注释

(E) examples

student table

create table student2( sid int not null primary key auto_increment comment '学号', sname varchar(10) not null comment '学生姓名', sage tinyint unsigned comment '学生年龄', sgender enum('m','f') not null default 'm' comment '学生性别', cometime datetime not null default NOW() comment '入学时间');

The field into a field behind

mysql> alter table stu add xmg int after ljk;

Remove a field

mysql> alter table stu drop ljk;

Modify the properties of the field

mysql> alter table stu change qls haoda int;

Modify field names and attributes

mysql> alter table stu modify qls char(10);

(Vi) DCL Data Control Language

1.grant

grant all on *.* to root@'%' identified by '1'; grant all privileges on *.* to pri2@'%' identified by '1'; grant all on *.* to root@'%' identified by '1' with max_user_connections 1;

revoke

mysql> revoke select on *.* from pri1@'%';

(Vii) DML Data Manipulation Language

1. increase

insert

#注意:所有值必须一一对应,如果没有就给null 
mysql> insert into student2 values(null,'qls',18,'m',now()); 
#注意:只需要给前面的key添加value,前面key值的顺序可以随意,后面value必须对应
mysql> insert into student2(sname,sage,sgender) values('zls',18,'m');
mysql> insert into student2(sage,sname,sgender) values(18,'zls','m'); 
#插入多条数据 mysql> insert into student2(sname,sage,sgender) values('zls',18,'m'),('qls',18,'f');

2. change

update

mysql> update student2 set sgender='f'; #规范用法 必须接where条件 
mysql> update student2 set sgender='f' where sid=1; mysql> update student2 set sage=20 where 1=1;

3. deleted

delete

# 必须接条件 mysql> delete from student2 where sid=2; mysql> delete from student2 where sid>3 and sid<9;

4. Charles

mysql> select * from student2 where state=1;

Use update instead of doing pseudo deleted **

1. Add a status bar

mysql> alter table student2 add state enum('1','0') default '1';

2. Use the delete update

mysql> update student2 set state='0' where sid=9;

3. When the query access conditions

mysql> select * from student2 where state=1;

Guess you like

Origin www.cnblogs.com/223zhp/p/11905909.html