《Mysql基础》【Mysql函数 mysql数据类型】 编程入门 学习分享 【公开免费】
-- -- mysql数据库程序设计笔记:
gb2312是国标,中国字库。一个汉字2个字节。 utf8国际通用标准。包含gb2312; 外键只能引用主键和候选键。 外键只可以在InnoDB中使用。 字段约束:字段类型后可加:check(多个列判断条件) 列为:column用 col1、col2....代替 一、mysql函数: 聚合函数: 1、count()函数:记录总数 select (*)from table 2、sum():总和。 select sum(col) from table; 3、avg():平均值 select avg(col) from table; 4、max():最大值 select max(col) from table; 5、min(): 最小值: select min(col) from table; 数学函数: 1、abs():绝对值:只返回整数。abs(-2)返回2 ,abs(2) 返回2. select abs(num), abs(-num); 2、floor():最大整数: 例如:select floor(1.5), floor(-2); //结果:1 ,-2 3、rand():0到1之间随机数。16位小数。 例如:select rand(); 4、truncate(x,y):返回数值x,保留y位小数。 例如:select truncate(2.33455,2);//2.33 5、sqrt(x):平方根: select sqrt(16);//4 字符串函数: 1、upper(s)或ucase(s):把s变为大写。 如:select upper('s');//S select ucase('s');//S 2、left(s,n):函数:返回字符串s的前n个字符。 如:select left('info',2);//in 3、substring(s,n,len):字符串s,n位开始,截取len长度的字符串。 如:select substring('projectinfo',2,3);//roj (第二位是r)(从1开始数,不是从0开始数。) 日期函数: 1、curdate() 或 current_date():获取当前时间年月日: 如:select curdate(); //2012-02-03 select current_date(); //2012-02-03 2、curtime() 或 current_time():获取当前时间时分秒: 如:select curtime(); //12:23:11 select current_time(); //12:23:11 3、now(): 获取年月日时分秒: 如:select now(); //2012-02-03 12:28:14 其他函数: 1、if(expr,v1,v2):条件判断函数,如果expr条件成立返回v1,否则返回v2. 如:select if(2>3,'true','false');//false select if(2<3,'true','false');//true 2、ifnull(v1,v2)条件判断函数:v1为不为空返回v1,否则返回v2. 如:select ifnull(1,'null'); //1 select ifnull(1/0,'null'); // null 二、数据类型: 1、数值型: bit (1-64) int (-21亿多到21亿多) smallint(0-65536) integer (int同义词) bigint (超大整数) double浮点小数 (最多小数位后15位,) 使用8个字节存储。 float单精度小数:(最多小数位后6位)使用4个字节存储。 举例保留2位:float(18,2),或:double(20,2) decimal (最多小数位后30位)(存储空间更优,更小,以插入的数值大小为准) (小数常用decimal类型) 举例:decimal(20,2) dec: decimal同义词 float、double、decimal 都符合四舍五入。 位数: bit->smallint->int->bigint-> 2、日期: date 年月日(1000-01-01到9999-12-31) datetime 年月日时分秒 (1000-01-01 00:00:00到9999-12-31 23:59:59) timestamp 时间戳(1970-01-01 00:00:00到2037年) time 时分秒 year 2位或4位格式的年 3、字符串: char: (0-255) varchar(0-65535) tinytext(255) text 65535 longtext