创建数据库
1 | create database dbname; |
删除数据库
1 | drop database dbname; |
选择数据库
1 | use dbname; |
创建表
1 2 3 4 5 6 7 8 | CREATE TABLE IF NOT EXISTS `book`( `id` INT UNSIGNED AUTO_INCREMENT, `title` VARCHAR(100) NULL, `author` VARCHAR(40) NOT NULL, `date` DATE, INDEX title_index(title(100)), PRIMARY KEY ( `id` ) )ENGINE=InnoDB; |
- AUTO_INCREMENT定义列为自增的属性,默认自增1,一般用作主键
- NOT NULL指定该字段不能为空, 在操作数据库时如果输入该字段的数据为NULL ,就会报错
- PRIMARY KEY关键字用于定义列为主键
- ENGINE 设置存储引擎
- CHARSET 设置编码
- INDEX设置列为普通索引、也可以使用UNIQUE指定唯一索引、 FULLTEXT指定全文索引
添加表字段
1 | alter table book add press varchar; |
删除表字段
1 | alter table book drop press; |
修改表字段
1 | alter table book modify press char; |
查看表结构
1 | show columns from book; |
修改表名
1 | alter table book RENAME TO book1; |
删除表
1 | drop table book; |
创建索引
1 2 3 | create index title_index ON book(title(100)); alter table book ADD INDEX title_index(title); |
删除索引
1 | drop index title_index ON book; |
插入数据
1 2 3 4 5 6 7 | insert into book values(值1,值2,....);#值的顺序与字段在表中的顺序一致
insert into book(title,author)
values('Java学习路线','Java学习录');#为指定字段赋值
insert into book(title,author)
select title,author from test;#copy另一张表的数据
|
修改表数据
1 | update table set title='学习笔记',date='2019-05-21' |
删除表数据
1 2 3 | truncate table book;#删除表中所有数据 delete from book where xx=yy;#根据条件删除表中数据 |
查询
1 | select * from book; |
常用函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | character_length("title") #字符串title的字符数
concat("title","author") #将字符串title和author合并为一个字符串
concat_ws(",""title","author") #将字符串title和author合并为一个字符串,合并时使用逗号作为分隔符
lower("title") #将字符串title的内容转为小写
upper("title") #将字符串title的内容转为大写
reverse("title") #反转字符串title
abs("price") #求price的绝对值
avg("price") #求price的平均值
count("price") #求price的总记录数
max("price") #求price的最大值
min("price") #求price的最小值
sum("price") #求price的和
rand()#返回0-1之间的随机数
adddate("date",n) #date加上n天的时间
addtime("date",n) #date加上n秒的时间
curdate() #当前日期
current_time() #当前时间
current_timestamp() #当前日期时间
datediff(d1,d2) #d1和d2相隔的天数
period_diff(d1,d2) #d1和d2相隔的月数
subdate(d,n) #d减去n天的日期
subtime(d,n) #d减去n秒的时间
connection_id()#服务器当前连接数
|
case when
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | CASE expression
WHEN condition1 THEN result1
WHEN condition2 THEN result2
..
WHEN conditionN THEN resultN
ELSE result
END
#CASE 表示函数开始
#END 表示函数结束
#如果 condition1 成立
#则返回 result1
#如果 condition2 成立
#则返回 result2
#当全部不成立则返回 result
#而当有一个成立之后,后面的就不执行了
|
IF
1 | IF(expr,v1,v2)#如果表达式 expr 成立,返回结果 v1;否则,返回结果 v2。 |
IFNULL
1 | IFNULL(v1,v2)#如果 v1 的值不为 NULL,则返回 v1,否则返回 v2 |
去除结果集中的重复元素
1 | select distinct(title) from book; |
模糊查询
1 2 3 | select * from book where title="Java%";#查询以Java开头的数据,只有这样使用索引以下两种不使用索引 select * from book where title="%Java";#查询以Java结尾的数据 select * from book where title="%Java%";#查询包含Java的数据 |
合并结果集
1 | select title from book1 union select title from book2 |
连接(left jion/right jion/jion/逗号)
1 2 3 4 | select * from book1 left jion book2 where xx=yy #获取左表所有记录,即使右表没有对应匹配的记录 select * from book1 right jion book2 where xx=yy #获取右表所有记录,即使左表没有对应匹配的记录 select * from book1 jion book2 where xx=yy #获取两个表中字段匹配关系的记录 select * from book1 , book2 where xx=yy#同jion |
分组
1 | select count(*) from book group by author ='Java学习录' #查询公众号Java学习录一共写了多少篇文章 |
排序
1 2 | select * from book order by date ASC#默认就是ASC 可省略,按date升序排列 select * from book order by date DESC#按date降序排列 |
分页
1 2 3 | select * from table limit 5; #返回前5行 select * from table limit 0,5; #同上,返回前5行 select * from table limit 5,10; #返回6-15行 |
推荐阅读
博客所有文章首发于公众号《Java学习录》转载请保留
扫码关注公众号即可领取2000GJava学习资源

8130

被折叠的 条评论
为什么被折叠?



