1、安装:安装后测试,show databases;
创建数据库:create databases 名; create database if not exists 名;
使用数据库:use 名;
查看编码:show variables like '%character%';
修改编码:alter database 名 character set....;
删除数据库:drop database 名;
drop database if exists 名;
创建表:create table 名(id int,name varchar(50));
添加:insert into 名(id,name) values(123,'张三');
查询:select * from 名;
删除:delete from 名 where id='123';
修改:update 名 set 列名='值',............where 列名=值
复制表:create table 名 select * from db_test.tb_test;将得到一个tb_test2的tb_test表的一个副本
查看数据库中所有表:show tables;
查看表结构:describe 名; desc 名;
删除表:drop table 名;
添加列:alter table 名 add column 列名 类型;
删除列:alter table 名 drop 列名;
2、 数据类型:tinyint 默认有符号(-128,127) unsigned 无符号(0,255)
smallint:大整数值 int:整型
float:单精度 bigint:极大整值
double:双精度 decimal(3,2)三位两个小数
3、时间和日期型:
Date:YYYY-MM-DD time:HH:MM:SS
year:年份YYYY Datetime:混合YYYY-MM-DD HH-MM-SS
timestamp:YYYYMMDD HHMMSS可以默认 now():当前时间
4、字符串类型:char、varchar、binary、varbinary、blob、text、envm、set、tinyblob、blob、mediumblob、logngblob、 tingtext短文本、text长文本、mediumtext中长度、longtext极大文本
5、数学函数:
abs(-3):绝对值 ceiling(x):>x的最小整数值
bin(x):返回二进制loct八进制,hex十六进制 exp(x):返回e(自然对数的底)的x次方
floor(x):<x的最大整数 greatest(,,,):最大值
least(,,,)最小值 ln(x):x的自然对数
log(x,y):x的以y为底的对数 mod(x,y):x/y的模
pi():pi的值(圆周率) rand():随机数生成器,生成一个指定的值
round(x,y):参数x的四舍五入的有y位小数的值 sign(x):返回代表数字x的符号的值,0 整数 负数(0,1,-1)
sqrt(x):平方根 truncate(x,y):数字x截短为y位小数的结果
nvl(e1,e2):如果e1为null,返回e2 nvl2(e1,e2,e3):如果e1为null,则返回e3,不为null返回e2
6、聚合函数:
avg():返回指定列的平均值 count():指定列中非空null值的个数
min():指定列最小值 sum():指定列所有值之和
group-concat()由属于一组的列值连接组合而成的结果 decode():函数将查询结果译为其他值
decode(column,值,译值,缺省值):
7、字符串函数:
ascii(char):字符串的asc[]i码值 bit_length(str):返回字符串的比特长度(位)
concat(,,,,):连城字符串,并用sep字符串间隔 instr():str从x处,y个字符长的子串替为instr
find_in_set(str,list):分析逗号分隔的list表,返回str位置 lcase(str)/lower(str):str中所有字符改变为小写
left(str,x):str中最左边的x个字符 length(str):str中的字符数
ltrim(str):str中切掉开头的空格 quote(str):反斜杠转译str中单引号
repeat(str,srchstr,rplcstr):str重复x次的结果 reverse(str):颠倒字符串str的结果
right(str,x):字符串最右边的x个字符 rtrim(str):str尾部的空格
strcmp(s1,s2):比较s1和s2 trim(str):去除str首尾的所有空格
ucase(str)/upper(str):str中转为大写
8、日期和时间函数:
curdate()/current_date() :当前的日期
curtime()/current_time():当前时间
current_timestamp():时间戳
date_add(now(),interval(2 month)):当前时间加两个月
date_sub(now(),interval(2 month)):当前时间减两个月
dayofweek(now()):一周中的第几天
dayofmonth(now()):一月中的几天
dayofyear(now()):一年中的几天
dayname(now()):星期名
hour(now()):小时
monthname(now()):月份名
quarter(now()):第几个季度(1-4)
week(now()):第几周(0-53)
9、格式化函数
date_format(now(),'%y/%m/%d %h/%i/%s')
10、系统信息函数
database():返回当前数据库
version():返回mysql服务器版本
benchmark(2,expr):将表达式expr运行2次
connection_Id():返回当前客户链接的id
found_rows():最后一个select查询进行检索的总行数
user()/system_user():当前登录用户名
11、算数运算符
+,-,*,/,%/mod
12、比较运算符
=,<>/!=,<=>安全的等,<,<=,>,>=,between.....and.....,in....,is not不是,is是,like '%孙%',not like
13、逻辑运算符
not/!,and/&&,or/||,xor异或,max(),min(),
14、自然连接
select class.classname,student.name from class,student where student.classid=class.id and student.classid=3;
15、自连接
select s.name from student s,student t where a.id=t.id and s.grade>90 and t.classid=3;
16、左外链接
select student.*,class.classname from student left outer join class on student.classid=class.id
17、右外连接
select student.*,class.classname from class right outer join student on class.id=student.classid
18、等值连接
select student.*,class.classname from student,class where student.classid=class.id
19、内链接
select student.*,class.classname from student inner join class on student.classid=class.id
20、五类约束
not null:非空约束
unique:唯一约束,某列或几列组合的数据不能重复
primary key:主键约束,某列数据不能重复唯一
foreign key:外键
check:检查,指定一个表达式,用于检查指定数据(不起作用)
21、取消非空约束
alter table test modify id int null;
22、增加非空
alter table test modify id int not null default 1;
23、创建唯一约束
id int not null unique
constraint UK unique(id)
多列:constraint UK unique(id,password)组合不重复
删除:alter table user drop index UK
增加:alter table user add constraint UK unique(id)
修改:alter table user modify id int unique
24、创建主键
id int primary key
constraint UK primary key(id)
删除:alter table test drop primary key
添加:alter table test add constraint PK primary key(id)
修改:alter table test modify id int primary key(id)
25、主键自增
auto_increment
id int auto_increment primary key