MySQL数据库基本sql语句

本文详细介绍了MySQL中的不同存储引擎(如InnoDB、MyISAM和MEMORY),以及事务的特性。此外,还涵盖了SQL语句的增删改查、DDL、DML、DCL和DQL操作,表的约束,配置文件设置,索引管理和用户权限管理,以及字符集的相关知识。
摘要由CSDN通过智能技术生成

(1)数据库引擎:

InnoDB存储引擎(默认引擎,最常用),MyISAM存储引擎,MEMORY内存型引擎

如何选择引擎:如果要提供提交、回滚、并要求实现并发控制,InnoDB是一个好的选择;如果数据表主要用来插入和查询记录,则MyISAM引擎能提供较高的处理效率;如果只是临时存放数据,数据量不大,并且不需要较高的数据安全性,可以选择将数据保存在内存中的Memory引擎;MySQL中使用该引擎作为临时表,存放查询的中间结果;

(2)存储引擎查看:

mysql> show engines;

修改搜索引擎:

ALTER TABLE 表名 ENGINE=引擎;

查看当前默认存储引擎:

mysql> show variables like '%storage_engine%';

 创建时指定引擎:

mysql> create table t1(id int,manager char(10)) engine =innodb;

(3)事务的特性:

        原子性,一致性,隔离性,持续性

(4)SQL语句:

增删改查:

DDL(Database Define Language)数据库定义语言:create,drop,alter

DML(Database Manage Language)数据库操纵语言:insert,delete,update

DCL(Database Control Language)数据库控制语言:grant,revoke

DQL(Database Query Language)数据库查询语言:select

create database database_name;                                //创建库

create table table_name(id int,name char(9));  //创建表

create table table_name select * from t1;                    //复制表,但是key不会被复制

create table table_name (select id,name from t1);       //复制单个字段和记录

desc table_name;                                                           //查询表结构

insert into table_name values(1,2,3);                           //往表中写入内容

select * from table_name;                                            //查看表内容

show table status like table_name\G                           //查看表的状态

rename table old_name to new_name;                       //修改表名称

alter table old_name rename new_name;                   //修改表名称

drop table table_name;                                                //删除表

drop database database_name;                                  //删除库

delete from table_name where id=1;                           //删除表中某一行数据

update table_name set name='liming' where id=3;     //更新表中数据

alter table table_name add english int;                       //添加字段

alter table table_name add name char(9) after id;      //添加字段并指定位置

alter table table_name add name char(9) first;           //添加字段并指定位置

alter table table_name change id id int not null;          //重新定义字段

select id,name from t1 where english>80;                   //条件查询where

select id,name from t1 where chinese>80 and english>80;    //多条件查询(and)

select id,name from t1 where chinese>80 or english>80;       //多条件查询(or)

select id,name from t1 where chinese between 80 and 100;   //多条件查询(between and)

select id,name from t1 where chinese in(60,70,80,90);           //IN集合查询

select id,name from t1 order by id;                              //默认从小到大排序

select id,name from t1 order by id desc;                     //降序排序展示 

select * from t1limit 5;                                                 //limit限制显示前5行

select id,name from t1 order by id desc limit 0,1;       //从第几行开始打印几行

select count(*) from t1;                                                //统计记录数量

select count(*) from t1;                                                //统计字段数量

select count(name) from t1 where salary>5000;         //统计薪资大于5000的有多少人

select count(name),post from t1 group by post;          //分组查询

select name,sum(singin) as singin_count from t1 group by name with rollup;    //分组查询并计算总和

select count(name),group_concat(name) fromt1 where chinses>80;    //条件查找并列出总数和分别是谁

select id,name,age from t1,t2 where t1.class=t2.class;    //多表查询

表的约束:主键(primary key),自增(auto_increment),unique key(字段的值不能重复但可以为空),unsigned(整型类型不允许为负值,正数上限提高一倍)

create table table_name(id int primary key);               //创建表并指定约束

create table table_name(id int,name char(9),primary key(id));        //创建表并指定约束

alter table table_name drop primary key;                    //删除主键

(5)配置文件设置默认字符集

来到MySQL的安装目录下找到my.cnf文件

在[mysql]下方添加一句:

default-character-set=utf8

在[mysqld]下方添加两句:

collation-server=utf8_general_ci

character-set-server=utf8

show variables like '%char%';                                     //查看字符集

set global 要修改的字段=utf8;                                    //将字段的字符集设置为UTF-8

create table t1(id int,name char(9),age int default charset=utf8);         //创建表时指定字符集

alter table table_name default character set utf8;         //修改表的字符集

(6)使用索引

create table student(id int,name char(9),age int,index nameindex (name));        //在创建表时指定索引

create index nameindex on student(name);                  //基于表结构创建

alter table student add index nameindex(name);          //修改表结构创建

show index from table_name;                                       //查看某个表格中的索引

drop index nameindex on student;                                //直接删除索引

alter table student drop index nameindex;                    //修改表结构删除索引

(7)创建用户修改权限:

create user liming@'localhost' identified by 'Liming@666';             //创建用户并设置密码

grant all on *.* to 'liming'@'localhost';                                              //给用户授权

grant all on *.* to 'liming'@'localhost' identified by 'Liming@666'   //创建用户设置密码并授权

flush privileges;                                                                               //刷新权限

revoke all privileges on *.* from liming@'localhost';                       //移除所有权限

revoke select,delete on *.* from liming@'localhost';                       //移除指定权限

set password='123456789';                                                            //修改密码

set password for liming@'localhost'='123456789';                         //修改其他用户密码

drop user 'liming'@'localhost';                                                        //删除用户(drop)

delete from mysql.user where user='liming' and host='localhost';  //删除用户(delete)

install plugin validate_password soname 'validate_password.so'; //安装密码强度插件

show variables like 'validate%';                                                      //查看密码强度

set global validate_password_length=1;                                        //修改密码强度

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值