当前位置: 移动技术网 > IT编程>数据库>Mysql > (办公)Mysql入门

(办公)Mysql入门

2018年10月22日  | 移动技术网IT编程  | 我要评论

数据库的操作:
1.用 show 显示已有的数据库
show databases
2.创建数据库:create database 创建数据库
create database db_name
3.删除数据库:
drop databse db_name
4.use 选定数据库.
use db_name
数据表的操作:
1.show/describe 语句显示数据表的信息:
show tables
2.create table 创建表:
create table 表名
(
列名 类型,
列名 类型
)
2.1 利用select的结果创建表.
mysql 将为在 select
2.1.1 create table 表名
(
select * from 表名
)
2.1.2 create table user_testc select * from user_t
2.2 利用alter table修改表
2.2.1 增加列: alter table 表名 add col_name 类型.
2.2.2 删除列: alter table 表名 drop col_name
2.2.3 改变列: alter table 表名 modify col_name 类型
alter table 表名 change old_col_name (新的列名)col_name 类型
2.3.4 给表更名: alter table 表名 rename 新表名.
3.drop table 删除表: drop table if exists 表名.
drop table if exists 数据库.表名

4.插入语句:insert
insert into 表(列名,列名,列名....) values(,,....)
4.1 插入其他表选择的行:
insert into tbl_name1(col1,col2) select col3,col4 from tbl_name2;
5.查询语句:select
select 语句的语法如下:
select selection_list 选择哪些列
from table_list 从何处选择行
where primary_constraint 行必须满足什么条件
group by grouping_columns 怎样对结果分组
having secondary_constraint 行必须满足的第二条件
order by sorting_columns 怎样对结果排序
limit count 结果限定
5.1 普通查询: select * from t_user
5.1.1 查询特定的行: select * from t_user where nickname like '%朱晓明%'
5.2 条件查询: select * from t_user where nickname like '%朱晓明%'
5.2.1 算术运算符:+,-,*,/,<,<=,=,!=或者<>,>=,>
5.2.2 逻辑运算符:not或!,or或||,and或&&
5.3 查询排序: order by 子句的语法 order by column_name [asc(升序)|desc(降序)][,....]
5.4 查询分组与行计数: select age,count(2) from person group by age
5.4.1 count()函数计数非null结果的数目.max(),min(),avg(),sum()
5.4.2 表连接,1.inner join,2.select * from tablea a,tableb b where a.bid = b.id
5.5 修改,删除数据记录. update 表名 set 列名 = xx where 列名 运算符 值
5.6 删除记录: delete from 表名 where 要删除的记录.
*********************************mysql函数字符串,索引*********************************
1.集合函数:
1.1 行列计数:count(*),计算查询语句返回记录数.
1.2 计算平均值:avg().对数字使用,忽略空值.
1.3 计算字段值的总和:sum()
1.4 计算字段的极值max()和min()
2.操作日期和时间.
2.1 返回当前的日期和时间curdate(),curtime()返回当前时间,以hh:mm:ss或hhmmss格式返回当前的时间值,now()
返回当前时期和时间以 yyyy-mm-dd hh:mm:ss 的格式或者 yyyymmddhhmmss 的格式.
2.2 使用关系运算符和逻辑运算符来限制时间范围:
select * from table where end_date >= '2001-02-08' and end_date < '2001-02-08'
2.3 另一种方法,你可以使用like来返回正确的记录.通配符'%'
2.4 比较日期和时间:to_days(date) to_days函数 返回一个天数 (从年份0开始的天数 )
3.字符串模式的匹配.
3.1:"_"匹配任意单个字符,"%"匹配任意数字字符。
3.2:扩展正则表达式模式匹配 regexp,not regexp
3.2.1 .匹配任何单字符
3.2.2 [...]匹配方括号内的任何字符.例如"[abc]",如果是范围的话-,[a-z]匹配任何小写字母,[0-9]匹配任何数字.
3.2.3 * 匹配零个或者多个在它前面的东西.比如[0-9]*匹配任何数量的数字,".*"匹配任何数量的任何东西.
"[aa]"匹配小写或大写的"a"而"[a-za-z]"匹配两种写法的任何字母。
3.2.4 为了定位一个模式以便它必须匹配被测试值的开始或结尾,在模式开始处使用"^"
或在模式的结尾用"$"。
4.深入select的查询功能.
4.1. 别名 select name as 别名 from table
在子句中使用列的别名:select count(1) total from table having total > 1

 

 



 

 

 

 

 

 


如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网