mysql基础
启动mysql net start mysql
关闭mysql net stop mysql
登录 mysql -uroot -p
退出 mysql>exit;
mysql>quit;
mysql>\p;
显示当前服务器版本 select version();
显示当前时间 select now();
显示当前用户 select user();
mysql默认的端口号是:3306
mysql超级用户是:root
创建数据库:create database
修改数据库:alter database
删除数据库:drop database
数据类型
整型:tinyint smallint mediumint int bigint
浮点型: float[(m,d)] double[(m,d)] m为数字总位数,d为小数点后面的位数
字符型:verchar(m)
数据表
查看有什么数据库:show databases;
打开数据库:use 数据库名称
查看当前数据库:select databases;
创建数据表:create table[if not exists]table_name(
agetinyint unsgined(无符号位),
......
)
查看数据表列表:show tables[form db_name];使用form可以查看别的数据库中的表
查看数据表结构:show columns form tbl_name;
插入记录;insert [into] tbl_name[(coi_name,...)] values(val,...);
查找记录:select expr,...form tbl_name(where .....);
空值与非空
create table[if not exists]table_name(
age1 tinyint unsgined(无符号位) not null,
age2 tinyint unsgined(无符号位) null,//默认是可以为空
......
)
自动编号auto_increment
1自动编号,且必须与主键组合使用
2默认情况下,起始值为1,增量为1
主键primary key
1每张表只能有一个主键
主键保证记录的唯一性
主键自动为not null
create table[if not exists]table_name(
age1 tinyint unsgined(无符号位) primary key,
......
)
唯一约束unique key
默认约束:default
更新记录update
语法:update tb_name set age=age+10 where name=chaihuo;
删除记录felete
语法:delete form tb_name where name=chaihuo;
查询结果分组group by
语法:select sex form users by sex;
hcaing分组条件
语法:select sex,age form users by sex having age>35;
主要:此时having后面只有两种情况1)聚合函数2)在select后面
对查询结果进行排序order by
语法:select * form users order by id desc;
运算符和函数
字符运算符
concat()字符连接
concat_ws()使用指定的分隔符进行字符连接
format()数字格式化
lower()upper()转换成小/大写字母
left() right()获取左/右侧字符
length()获取字符串长度
substring()字符串截取
[not] like 模式匹配
replace()字符串替换
数值运算符与函数
ceil() 进一取整
div 整数除法
floor()舍一取整
mod 取余数
power() 幂运算
round()四舍五入
比较运算符与函数
[not] between...and... [not]in() is[not]null
信息函数
connection_id() 连接id
datebase()当前数据库
last_insert_id()最后插入记录的id号
user()当前用户
verson()版本信息
聚合函数
avg()平均值
count()计数
max() min() sum()
加密函数
md5()信息摘要算法
password()密码算法
子查询和连接
将查询结果写入数据表
insert[into] tbl_name[(col_name,...)] select...
例子:insert tdb_goods(cate_name) select good_cake form table group by good_cake;
多表更新
update table1 inner join table2 on table1_name=table2_name set table1_id=table2_di;
create...select
创建数据表同时将查询结果写入到数据表
create table table_name [(create_definine)] select_statement
例:create table table1(
id smallint unsigned primary key auto_increment,
name varchar(20) not null)
select select good_cake form table group by good_cake;
)
存储引擎
通过修改mysql配置文件实现
1)-ddfault-storage-engine=engine
2)通过创建数据表命令实现
create table[if not exists]table_name(
age1 tinyint unsgined(无符号位) not null,
age2 tinyint unsgined(无符号位) null,//默认是可以为空
......
)engine=engine;
通过修改数据表命令实现
-alter table table_name engine[=]engine_name;
存储引擎
myisam:存储现在可达256tb,支持索引。表级锁定,数据压缩
innodb:存储限制为64tb,支持事务和索引。锁颗粒为行锁
以上就是mysql中的基础知识的详细内容。
