Mysql数据库常用语句

mysql的一些常用指令
查看创建数据库的指令:show create database 数据库名;
显示数据库指令:show databases;
删除数据库:drop database 数据库名
如何指定使用某个数据库:use 数据库名;

创建一张用户表
create table users (
id int ,
name varchar(64),
pwd varchar(64),
birthday date)

mysql 自带的client 默认支持 utf8 码,所有我们在添加中文的时候,需要设置让client支持gbk
* show variables like 'char%';   //显示关于字符的设置参数
* set character_set_client=gbk; //可以存中文
* set character_set_results=gbk; //可以看中文


create table test3(num tinyint) -- -128 到 127
create table test4(num tinyint unsigned) --  0 到 255 

FLOAT[(M,D)] [UNSIGNED]  是定长
m : 表示有效位
d: 表示小数点有几位
案例:
create table test5( num float);
create table test6(num float(5,1));
numeric(m,d)
用于表示小数,或者整数
create table test7 (num numeric); //这样其实就是可以存放整数.
create table test8 (num numeric(5,2));//这样就可以表示 有效为5,小数点有两位的数
char(m)
m 范围是 0-255, 定长.
char(20) 如果你存放 ‘abc’ 字串,实际在表 ‘abc              ’;
案例:
create table test11 (name char(20));
varchar(m)
m 表示大小 ,范围 0-65535, 变长
varchar(20) 如果你存放 ‘abc’ 字串,实际在表 ‘abc’;
建议: 如果表的某列长度固定,比如 产品编号..学号. .. 而且在 255内,我们应当使用char
,如果长度不能取得,或者长度 大于255 小于 65535 则使用varchar
date
日期 (年-月-日)
create table test12(birthday date);
对于date 只保存 年-月-日
datetime
日期时间类型
create table test13(hiredate datetime);
timestamp
邮戳: 该类型可以保存 年-月-日 : 时:分:秒
它和datetime 最大的区别是,当你 update 某条记录的时候,该列值自动更新
create table test14 (
 name varchar(64) ,
 sal float,
 hiredate1 timestamp,
 hiredate2 datetime);

--添加新的列
alter table 表名 add 列名 数据类型
--修改列(列的类型和大小)
alter table 表名 modify 列名 新的数据类型
--删除某列
alter table 表名 drop  列名
rename table 原表名 to 新表名
alter table 表名 character set 字符集名;
alter table user change column name username varchar(20);

create table emp(
id int,
name varchar(64),
sex char(2),
birthday date,
Entry_date date,
job varchar(32),
salary float,
resume text)

在上面员工表的基本上增加一个image列。
alter table emp add image blob;
修改job列,使其长度为60。
alter table emp modify job varchar(60);
删除sex列。
alter table emp drop sex;
表名改为user。
rename table emp to user;
修改表的字符集为utf-8
alter table user character set utf8;
列名name修改为username
alter table user change column name username varchar(30);
如何显示创建表的指令:
show create table 表名;

create table test15 (name varchar(64));
insert into test15 (name) values(‘aaa’);
insert into test15 (name) values(34);
字符和日期型数据应包含在单引号中。

将所有员工薪水修改为5000元。
update employee set sal=5000;
将姓名为’zs’的员工薪水修改为3000元;
update employee set sal=3000 where name=’zs’;
将wu的薪水在原有基础上增加1000元
update employee set sal=sa+1000 where name=’wu’;

delele from 表名 where 条件;
使用delete语句仅删除记录,不删除表本身。如要删除表,使用drop table语句。
删除表中数据也可使用TRUNCATE TABLE 语句,truncate table 表名,可以删除表的记录,速度快,但不能回滚..

mysql 控制台是默认自动提交事务(dml)
如果我们要在控制台使用事务,应该这样
set autocommit=false;
savepoint 保存点
//操作...
rollback to 保存点.

过滤表中重复数据。
select distinct * from 表名
查询英语分数在 80-90之间的同学。
select * from student where english>=80 and english<=90;
查询数学分数为89,90,91的同学
select * from student where math in (89,90,91);
查询所有姓李的学生成绩。
select * from student where name like ‘李%’;
查询数学分>80,语文分>80的同学。
select * from student where matn>80 and chinese>80;
统计一个班级数学总成绩?
select sum (math) from student;
求一个班级数学平均分?
select avg (math) from student;
查询购买了几类商品,并且每类总价大于100的商品
select product , sum(price) from orders group by product having sum(price)>100

select current_date() from dual ; 得到当前日期
select current_time() from dual ;  得到请求时间;
创建一个留言表
create table message(id int , title varchar(64), publishdate datetime);
查询出两个小时内发布的消息:
select * from message where  date_add (publishdate, interval 2 hour) >= now();

把 ename 列 的 smiTh 第一个字母大写,其它全部小写
select concat (lcase (substring('smiTh',1,1)), ucase (substring('smiTh',2,length('smiTh')-1))) from dual;

创建一个雇员表:
create table emp(id int primary key,
name varchar(32),
deptid int ,
constraint emp_fk foreign key (deptid) references dept(id)
);

mysql 分页查询:
返回第 4条 ----第 7条记录
select * from student limit 3,4;
基本语法
select * from 表名 where 条件 ... limit 从第几条取,取出几条
select * from 表名 where 条件 [group by .. having .. order by ..] limit (pageNow-1)*pagesize, pageSize;

猜你喜欢

转载自chenzheng8975.iteye.com/blog/1665695