1、创建数据库
create database mainContent
2、使用数据库
use mainContent
3、删除数据库
drop database mainContent
4、创建表
<1>varchar:定长可变的
<2>char:定长不变的
注:在此次的表单建立中,使用命令行建立中总是出现错误,在找不到解决办法的时候,于是直接使用localhost:8089/phpmyadmin进行可视化界面的数据表建立,但在最后的建表中,也遇到了同样的问题,于是发现user_id默认应该建立为null,具体原因还在理解中。
5、显示表
show tables;
6、查看表结构
show columns from users;
7、插入数据
insert into users(first_name,last_name,email,paass,registration_date)
->values ('liu','yanbo','Lambo_888@163.com','SHA1('mypass'),NOW()');
8、查看所有元素
select * from users
9、where条件语句
select * from users where first_name='li' \G;
注:如果没有加\G的话,显示 如下图所示:
注:为什么where中的user_id显示NULL,最主要的是在可视化创建的时候,选择AUTO_INCREMENT,但最主要的是需要进行“索引”的选择设置。
10、between...and....
select first_name from users where user_id between 2 and 4;
![](https://i-blog.csdnimg.cn/blog_migrate/7192014bce5f8d76bef442d17a0ed380.png)
11、LIKE/NOT LIKE
1\通配符_:匹配单个字符;%:匹配0个或者多个字符。
2、在通配符前加上(\),则_或%只作为原始符号,不具有通配符意义。
3、带有LINK条件语句的查询一般比较慢,因为不能利用索引,所以仅当绝对需要时才可以使用。
12、排序order by 升序ASC,降序DESC
若要排序的列是ENUM类型,排序会基于列创建ENUM值时的顺序。
select * from users order by user_id DESC;
select first_name from users order by first_name ASC;
13、limit
<1>limit X:返回查询记录中的前X条记录
<2>limit X,Y:返回从X条记录开始的Y条记录
<3>limit子句不会改进查询的执行速度,因为mysql仍然必须把每一条记录集合到一起,然后截短列表
14、update修改表中的数据
<1>、防止自己意外更新过多的行,可以对update应用一个limit子句。
<2>、清空一个表,首选truncate:truncate table tablename
<2>、删除表中所有数据以及表本身,drop table tablename
<3>、删除整个数据库,drop database databasename
select first_name from users where first_name='liu';
update users set first_name='Liu' where first_name='liu';
select first_name from users where first_name='Liu';
![](https://i-blog.csdnimg.cn/blog_migrate/02c9e8d5a2fa4838c5a3355ac1469580.png)
15、concat连接函数
select concat(first_name,'-',last_name) from users;
select concat(first_name,'-',last_name) as name from users;
![](https://i-blog.csdnimg.cn/blog_migrate/e6f7120ce99a8af4bf5bd9bdc89f96fa.png)
16、concat_ws连接函数
select concat_ws(',',first_name,last_name) as name from users;
![](https://i-blog.csdnimg.cn/blog_migrate/b23529f50e5bb7fcfaecba4a7763d32c.png)
注:length是一个计算字段长度,一个汉字算三个字符,一个数字字母算一个字符
18、format函数
format函数在mysql中是数据内容格式化的,格式化后得到的结果###,###,#####
<1>select format(100000,2);
输出结果:
100,000.00
<2>可以格式化数据位整数或者浮点数
select format(100.31111,2)
输出结果:
100.31
<3>select format(100.31111,0)
输出结果:
100
<4>具有四舍五入的功能
select format(423423234.65534555,2)
输出结果为:
423,423,234.66
![](https://i-blog.csdnimg.cn/blog_migrate/a56744bfa50c2216d31c8ec5b4cf9439.png)
19、concat和format函数合用
![](https://i-blog.csdnimg.cn/blog_migrate/afc7399562e5d4616dacc2d00d8dc26b.png)
20、rand()函数
![](https://i-blog.csdnimg.cn/blog_migrate/d61efd51834290ee9bf1b51ecbf71cfe.png)
21、now(),curtime(),curdate(),时间函数
![](https://i-blog.csdnimg.cn/blog_migrate/dee4851939860326ec56a410a83e7c73.png)
22、
date_format函数