Mysql常用语句
1.字符集问题
mysql的数据库中的表里填写不了中文
解决办法
ALTER TABLE 数据库.表名 CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
#两个方式
ALTER DATABASE 数据库 CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE 数据库.表名 CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
2.常用sql语句
1.简单的登录sql,创建database
mysql -uroot -p123456 --连接数据库
update mysql.user set authorization_string=password('123456')where user='root' and Host = 'localhost';
--修改用户密码
flush privileges ;--刷新权限
----------------------------------------------------------------
--两个小横杠代表注释
--所有语句都使用分号;结尾
create database sai_mysql;--创建数据库
--Query OK, 1 row affected
show databases like "com.sai%";--查询包含sai的数据库(应该有条件,目前不知道详细内容)
-- +-----------------+
-- | Database (com.sai%) |
-- +-----------------+
-- | sai_mysql |
-- +-----------------+
drop database sai_mysql;--删除数据库
-- Query OK, 0 rows affected
2.使用database,进行table的操作
use sai_mysql;--使用数据库
-- Database changed
create table name(name varchar(10)) charset utf8;--创建表
-- Query OK, 0 rows affected
describe name;--显示数据库中的表的所有信息
desc name;
show columns from name;
-- +-------+-------------+------+-----+---------+-------+
-- | Field | Type | Null | Key | Default | Extra |
-- +-------+-------------+------+-----+---------+-------+
-- | name | varchar(10) | YES | | NULL | |
-- +-------+-------------+------+-----+---------+-------+
alter table name charset utf8;--设置表的属性
-- alter table 表名 charset 字符集名;
---------------------------------------------------------------
--修改
rename table name to stu;--修改表的名字
alter table stu add column age int first;--新增字段
-- alter table 表名 add column 新字段名 列类型 位置[first/after 字段名];
alter table stu change name names varchar(10) first;--修改字段名
-- alter table 表名 change 旧字段名 新字段名 字段类型 位置[first/after 字段名];
alter table stu modify names varchar(20) first;--修改字段属性
-- alter table 表名 modify 字段名 新类型 位置[first/after 字段名];
----------------------------------------------------------------
--删除
alter table stu drop names;
-- 删除字段: 基本语法–>alter table 表名 drop 字段名;
drop table stu;
-- 删除表结构: 基本语法–>drop table 表名,表名2,表名3;
----------------------------------------------------------------
--插入数据
insert into stu (name,age) values ("sa_i",16);
-- 插入数据: 基本语法–>insert into 表名 (name,age) values ("jack",30);
-----------------------------------------------------------------
--查询
select * from stu;
-- 查询表中全部数据: 基本语法–>select * from 表名;
select name from stu;
-- 查询表中部分字段: 基本语法–>select 字段列表,字段列表名2 from 表名;
select * from stu where age = 20;
-- 简单条件查询数据: 基本语法–>select 字段列表/* from 表名 where 字段名 = 值;
----------------------------------------------------------------
--删除操作: delete from 表名 where 字段名 = 值;
delete from stu where age = 30;
----------------------------------------------------------------
--更新操作: updata 表名 set 字段名 = 新值 where 字段名= 值;
update stu set name = "tom" where age = 20;
----------------------------------------------------------------
--备份
--整库备份: 基本语法–>mysqldump.exe -hlocalhost -P3306 -uroot -proot 数据库名 > 保存目录
mysqldump.exe -hlocalhost -P3306 -uroot -p123456 sai_mysql > f:/SAI_newSQL/sai_mysql.sql
--数据库表备份: 基本语法–>mysqldump.exe -uroot -proot 库名 表名1 表名2> 保存路径
mysqldump.exe -uroot -p123456 sai_mysql stu> f:/SAI_newSQL/stu.sql
3.排序、查询分页、模糊查询
#排序
SELECT * FROM student ORDER BY stuTotalScore;
#默认升序,若要降序在结尾加desc
SELECT *
FROM student
ORDER BY stuTotalScore DESC;
#多列排序
SELECT *
FROM student
ORDER BY stuTotalScore,stuJavaScore;
#查询表中前5个数据
SELECT s.`stuID`,s.`stuName`,s.`stuTotalScore`
FROM student s
LIMIT 5;
#查询第10个到第15个student的数据
SELECT s.`stuID`,s.`stuName`,s.`stuJavaScore`,s.`stuPythonScore`,s.`stuTotalScore`
FROM student s
#9代表从第10个开始查询,6代表查询6个人
LIMIT 9,6;
#另一种写法
SELECT * FROM student LIMIT 6 OFFSET 9;
#分页
SELECT COUNT(*) FROM student;#获取数据的总条数
#模糊查询
SELECT * FROM student WHERE stuName LIKE '%李%';#名字中有李
SELECT * FROM student WHERE stuName LIKE '张%';#张字开头
SELECT * FROM student WHERE stuName LIKE '%黄';#黄字结尾