mysql数据库 经常使用操做 sql指令 总结

建立数据库 crate database name;

建立一个值得字符集的数据库mysql

create database name character set utf8;  //指定utf8类型的数据库sql

更改字符集 alter database name character set gbk;  //改成了gbk字符集数据库

建立校对规则: 用于排序 如张三李四 能够按照拼音、笔画排序服务器

create database name character set utf8 collate uff8_unicode_ci;函数

显示建立数据库 show databases;

切换数据库 use name;  //切换到name数据库

查看当前正在应用的数据库 select database();  //显示出当前数据库 名字

删除数据库 drop database name;


表建立操做

create table users(表名)(大数据

   id int,编码

   name varchar(40),spa

   password varchar(40),code

   birthday dateorm

);

表的数据类型中,除了char 、varchar 必须指定长度,其它类型都有默认长度 


mysql经常使用的数据类型

字符串型            VARCHAR、CHAR

                         char定长 char(8) ---- 向数据库存入hello  存为hello+3个空格

⼤大数据类型     BLOB、TEXT                    

                        text 文本类型数据,主要存储字符文件 --- 文本文件

                        blob 二进制文件 ,存储任何类型文件(音乐、电影);longtext longblob 最大能够保存4GB文件 

数值型               TINYINT、SMALLINT、INT、BIGINT、FLOAT、DOUBLE 

                         tinyint (byte)  smallint(short) int(int) bigint(long)

逻辑性               BIT  

                         bit(8)表示8位 bit(32)表示32位至关于 int

日期型           DATE、TIME、DATETIME、TIMESTAMP                       

                        date 只能保存日期

                        time 只能保存时间

                        datetime 日期和时间都有

                        timestamp 日期和时间都有,自动更新到当前时间

经过desc语句 查看表结构

语法:desc 表名; 

单表约束条件:

主键约束(惟一标识一条记录)---- primary key 不能为空、不能重复  通常自动增加 mysql为 auto_increment

惟一约束(该字段内容不容许重复)----- unique 一张表只有最重要那个字段才能做为主键 

非空(值不能为空)----- not null

create table employee (

   id int primary key not null auto_increment ,

   name varchar(40) unique not null,

   gender varchar(10) not null,

);

表操做

向已有数据表添加一列 :                  alter table 表名 add 列名 类型(长度) 约束;

修改已有数据表一列类型、长度:     alter table 表名 modify 列名 类型(长度) 约束;

修改已有数据表一列的名称 :           alter table 表名 change 旧列名 新列名 类型(长度) 约束;

删除已有一列 :                                  alter table 表名 drop 列名;

修改表名:                                         rename table 旧表名 to 新表名; 

修改表的字符集:                            alter table student character set utf8;

表删除                                           drop table 表名;

show tables; 查看当前数据 中全部表

向表中添加一列:             alter table employee add image varchar(255);

修改一列的长度、类型      alter table employee modify job varchar(60) not null;

修改一列的名字                alter table user change name username varchar(40) unique not null;

删除一列。                       alter table employee drop gender;

修改表名。                       rename table employee to user;

修改表的字符集为             alter table user character set utf8;

show create table user; 显示表user的信息


表数据操做

表中插入数据

1,insert into employee(id,name,birthday) values(null,'zs','1990-01-10');字符串添加单引号 字符、日期单引号

2,insert into employee(name) values('zs');  //只插入部分

3,insert into employee values(null,'lisi',null);  //第一种简写

mysql有六处使用了字符集,分别为:client 、connection、database、results、server 、system。

服务器端相关:database server system(永远没法修改 就是utf-8)

客户端相关 connection client results 

解决中文插入乱码问题:将客户端相关三个编码集设置 gbk 

set names gbk; ----- 快速设置客户端相关三个编码集

修改mysql 配置文件,永久改变客户端编码集 ----- mysql/my.ini 

[mysql] ---- 客户端配置        //修改后注意重启mysql服务

[mysqld] ---- 服务器端配置 


记录修改操做

update 表名 set 列名=值,列名=值 where条件语句

update employee set salary = 3000 where name = 'zs';

条件比较前添加 binary 使比较更加精确严格,不然,'ZS '也会被当成‘zs'

update employee set salary = salary+1000 where name = 'wangwu';  //对salary进行加1000操做

没有where语句,对全部数据行进行更新 

update employee set salary = 5000 ;   //全部人的工资设为5000


表数据删除

delete from 表名 where 条件语句;

delete from employee where name = 'zs';

truncate employee; delete from employee;  //删除整个表数据

truncate 删除记录后不可恢复的,不受事务管理,原理:删除整个表,delete能够rollback;回滚恢复


表数据查找

SELECT *|column1, column2. columnFROM table;

select * from student;    //查询全部列

select name from student;  //查询名字列

select distinct name from student;  //查询结果 不显示重名,distinct去重复


查询时运算

select name,math+chinese+enlish from student;  //查询姓名 和总分 也能够加数字 math+10

查询时更改列名 as  ;as能够省略

select name as 姓名 from student;  //列 更名为 姓名

select name 姓名 from student;

查询时筛选 比较

select name from student where math > 90;

select name from studetn where name like '李%';取出全部姓李的

select name from studetn where name like '李_';取出姓李的 名字两个字的

and 和 or运算优先级 ; and的高,先执行

查询时排序 order by 列 asc|desc; asc升序 desc降序

select name from student order by math desc;  //按照数学高到低排序

表函数

count 返回查询结果的条数

select count(*) from student where math > 90;  //查询数学大于90 的人数

sum对一列求和

select sum(math) from student ;   //查询数学总分

avg 求一列的平均值

select avg(math) from student;    //求平均值  ;注意 null不参与运算

max,min 找出一列中最大最小的值

select max(math) from student;    //找出数学成绩最高的

表查询分组

group by 列; 筛选 having;

select name from student group by class having math > 80;  //选出数学大于80 的名字 按照班级分组


多表查询

 

内链接:将两张表相赞成义字段链接起来

语法:select * from a inner join b on a.id = b.id; 

简化:select * from a,b where a.id = b.id; 

外链接:用一个表的查询数据去查询另外一个表

表a                       表b

select * from a left outer join b on a.A_ID = b.A_ID ;   //先查询a表 而后 用a表 A_ID 去查表b 有写出,没有为null

select * from a right outer join b on A.A_ID = B.A_ID ;

全链接 mysql不支持 可使用union 合而且去掉重复的

select * from A left outer join B on A.A_ID = B.A_ID

union

select * from A right outer join B on A.A_ID = B.A_ID;


关联子查询:将第一个查询结果 ,做为第二个查询条件 

select name from student where age = (select max(age) from student);  //年龄最大学员的名字

相关文章
相关标签/搜索