/*
mysql基本操作
*/
端口(默认):3306
用户名(默认):root
mysql -uroot -p123 //账号密码进入
mysql -u root -p; //空密码进入,用户为root
show databases; //查看有哪些数据库
use my_database; //切换数据库
show tables; //展示表
net start mysql; //启动mysql
net stop mysql; //关闭数据库
select * from t_zhaopin\g; //按行垂直显示结果
drop table un_check_table; //删除表
create database my_database; //创建数据库
/*
建表
*/
NOT NULL: //非空
UNIQUE: //唯一约束,不能重复
PRIMARYKEY:主键约束
FOREIGNKEY:外键约束
char(size):固定长度的字符串,如声明char(10),定义数据类型为char时,不论真实数据多大,几位,在数据库中一律占用10字节
varchar(size):不定长字符串,有效长度最大只有255,若长度超过255,则默认使用text类型存储数据
text:不可设置长度,最大为65535字节,大约64KB,通常用户保存不含图片,音频,视频等附件信息的文本格式数据
longtext:加长版的text
blob:通常用于包含各类文件附件等,不具备标准
longblob:blob的加长版,最大允许约4GB的数据
int:更多用于计算
float(size,d):size为最大位数,d为小数部分位数
decimal:通常用于金融财务类型的浮点数据
create table meeting_room(
id integer primary key auto_increment, //自动递增
name varchar(90) not null, //非空约束
addr vachar(90) unique, //唯一约束,不能重复
foreign key(super_id) references foreign_key_super(id), //外键约束
age int check(age>0), //检查约束
tel varchar(20) default 'zhang', //为name设置默认值
);
create table new_teacher( //复制表(指定字段及数据)
select id,name,pro from student where id<100
);
alter table teacher add addr varchar(200); //添加列
alter table type_court_custom_identity add identity_color varchar(32) DEFAULT NULL COMMENT '身份颜色表示'
alter table teacher drop addr; //删除列
alter table teacher modify name varchar(20); //修改列类型
alter table teacher addr address varchar(200); //改列名
alter table teacher add primary key(id); //添加约束
alter table teacher add foreign key(pro) references professional(id); //添加主外键关系
/*
数据库函数
*/
select id,concat(year,'_',month,'_',day) as date from test_date; //查时间
select id,full_date from test_date where SUBSTRING;
select SUBSTRING(full_date,6,2) from test_date; //6:从1开始起第6位; 2:延伸两位
select id,substring(id_no,1,6) from student; //取id_no值,前6位
select id,length(id_no) from student where length(id_no)!=18; //查id_no长度
select id,substring(id_no,length(id_no)-3,4) from student; //取倒数四位
select id,left(id_no,6) from student; //取前6位
select id,right(id_no,4) from student; //取后4位
select id,replace(full_date,'_','/') from test_date; //指定列full_date的'_',被替换为'/'
select id,date,sign(money) from test_money; //若money>0,则1;若money<0,则-1
select id,date, case sign(money) when 1 then '盈利'
else '亏损'
end as s
from test_money;
select * from student order by rand() limit 5; //随机查询5条记录
trunncate table test_money; //清空表中数据,保留表
select dayofweek('20170515'); //返回2,西方国家以星期天为1
select weekday ('2017-05-15'); //返回0,0表示星期一
select dayofmonth('2017-05-15'); //返回15,这个月的第15天
select dayofyear('20170515'); //返回135,今年的第135天
select year('20170515'); //返回2017,返回2017
select month(20170515); //返回5,返回5月份
select week(20170515); //返回20,今天是第20周
select now(); //返回数据库服务器当前时间
select sum(cls) from student; //计算cls总和
select pro,group_concat(id) from student group by pro; //根据pro分组排序,列出各组id
round(d.holes_number/18,1) AS number
{
在mysql中,round函数用于数据的四舍五入,它有两种形式:
1、round(x,d) ,x指要处理的数,d是指保留几位小数
这里有个值得注意的地方是,d可以是负数,这时是指定小数点左边的d位整数位为0,同时小数位均为0;
2、round(x) ,其实就是round(x,0),也就是默认d为0;
下面是几个实例
1、查询: select round(1123.26723,2);
结果:1123.27
2、查询: select round(1123.26723,1);
结果: 1123.3
3、查询: select round(1123.26723,0);
结果:1123
4、查询: select round(1123.26723,-1);
结果: 1120
5、查询: select round(1123.26723,-2);
结果:1100
5、查询: select round(1123.26723);
结果:1123
}
mark:学习笔记