mysql
一,数据库简介
1.1,什么是数据库
数据库:数据库是按数据结构来组织,存储,和管理数据的仓库.是一种对大量信息进行管理的高效解决方案。
数据库系统(database system):包含数据库(database) 数据库管理系统(database management system)
1.2.数据库的分类
关系型数据库:mysql,Oracle,DB2,Microsoft SQL server,
非关系型数据库:NOsql,mongoDB,redis,Hbase
1.3 数据库的常用术语
数据库系统:DBS(database system)
数据库:DB(database)--仓库
数据库管理系统:DBMS(databasse management system)
table:表,数据库内的数据集合都放在表(二维表)内。
行:row,列:column,据表由数据行和数据列构成
记录:record 一个数据行就是一条记录
字段:field,录内的每个列,就是一个字段
SQL:Structured query language结构化查询语言,操作关系数据库的通用语言,用于执行数据的检索和其他操作
1.4 连接数据库
mysql -h(主机名) -p(端口) -u(用户名) -p(密码)
退出数据库:exit quit or q
二,数据库操作
2.1,创建数据库
语法:create database `数据库名字`;
#注:1),数据库语法都以分号结尾
2),mysql中,所有的标识符都以反引号包裹,如数据库名字,表名等
2.2 查询数据库
语法:
show databases;#查询所有的数据库
show databases like 'it%';--->查询以it 开头的的数据库
show database like '___';---->查询库名为3个字符的数据库
注:like 是对查询结果进行筛选,like 后面跟筛选条件,可以只保留需要保留的数据;%号表示占位,如like 后 跟'%ro',表示以ro结尾的数据库,like 后跟'%ro%',则表示查询库名包含ro的数据库._下划线表示一个任意字 符,用来表示按数据库名的字符个数查询.
show create database `库名`;查询创建数据库的语句
2.3,删除数据库
语法:
drop database 数据库名;
2.4,修改数据库(主要修改数据库的选项)
语法:
alter database 数据库名 选项=新的值;
例:alter database `itsource` charset=gbk,修改itsource 的字符集为gbk.
三,数据库中表的操作
3.1创建表
注:在创建表时,需先指定数据库
语法:use 库名;----->选择要操作的数据库
创建表的语法:
create table 表名(字段1 字段1类型 字段1的属性,
字段2 字段2类型 字段2的属性,
字段3 字段3类型 字段3的属性,)[选项]
例:
create table student(
id INT(10) null,
name varchar(10) not null,
sex char(4),
class_id int(10))engine=innodb charset=utf8 comment='备注';
#其中id 是字段名,int是字段类型,null是字段属性
注:
1,字段名就是指没一列的列标题
2,字段类型包括:
整数型:int(整数) tinyint(微整型)
小数型:浮点数:float(单精度浮点数),double(双精度浮点数)
定点小数 decimal(M,D):decimal(5,2)表示总共5位数,小数占2位,整数3位
字符串型:char(m)
varchar(m)------>m表示给定的字符位数
文本型:text
longtext
时间日期:
datetime 时间与日期
timestamp 时间戳 范围:1970-2038年
3,字段属性包括:
1),空
null:允许为空
not null:不允许为空
2),默认值
default 值:表示表中如果不输入数据,则显示默认值.
3)主键:
primary key :一个表中只能有一个主键,主键不能重复,用于唯一一个标识一条记录
4)自动增长
auto_increment 只能在主键中用
5)unique key 唯一,限制除了主键外的其它字段内的数据不能重复,一个表只能有一个主键,但可以有多个唯一键
6)comment备注
例:
create table student2(id int(10) unsigned primary key auto_increment,
name varchar(20) unique key,
age int(10))engine=innodb charset=utf8 comment='学生表';
4,选项里包含:
1)engine:引擎,表的存储类型,包含innodb,高级引擎,myisam,低级引擎.
2)charset:字符集,charset=utf8 or charset=gbk
3)comment:备注,用来说明表的信息.
3.2查看表
语法:
show tables;---->查看所有表
show tables like '%i%';查询表名包含i的表
show tables like '___';查询表名为3个字符的表
注:这里的%和_与查数据库的意义相同
show create table 表名;---->查创建表的语句
desc 表名;---->查看表的结构,可以查到表的字符段,字符段类型,属性
3.3 修改表
修改表的选项语法:
alter table 表名 新的选项
例:alter table student comment='学生表';---->将studnet表中的备注改为学生表
修改表名:
rename table 原表名 to 新表名
例:alter table studnet to studnet1;
给表添加新的字段:
alter table `表名` add column `字段名` 字段属性 after `字段名`;
例:alter table studnet add column sorce int after age;
删除表的某一字段:
alter table 表名 drop 字段名
例: alter table studnet1 drop sex;
修改表的字段类型
alter table 表名 modify 字段 属性选项;
例: alter table studnet1 modify id tinyint;
修改表的字段位置
alter table 表名 modify 字段 类型 after 字段;
例: alter table studnet1 modify class_id int(10) after id
修改表中字段的名字
alter table 表名 change 字段 重命名字段 字段属性;
例: alter table studnet1 change id stu_id int;
3.4删除表
drop table 表名;
四,表中记录的操作
4.1,增加,插入记录
insert into 表名(字段1,字段2,字段3,字段4)values(值1,值2 ,值3,值4);
例:insert into student(id,name,age,sex,class_id) values(01,'张三',19,'男',1001);
注:1,如果每个字段都传入值,可以不写字段名,写传入的值,语法:insert into 表名 values(值1,值2...);
例:insert into student values(01,'张三',19,'男',1001);
2,如果只传入部分值,就必须写字段名和值,且字段名和值要一一对应.
例如insert into student(name,age,sex,class_id) values('张三',19,'男',1001);
同时插入多条记录
insert into 表名(字段1,字段2,字段3,字段4)values(值1,值2,值3,值4),(值1,值2,值3,值4),(值1,值2,值3,值4),(值1,值2,值3,值4);
例:insert into student values
(05,'溪云',19,'女',1004),
(06,'成岩',18,'男',1005),
(07,'渔火',18,'女',1004),
(08,'阮七七',20,'女',1003),
(09,'周深',19,'男',1005);
4.2 查询记录
select * from 表名;---->查询表中的所有记录.
例: select *from student;
select * from 表名 where 条件表达式;----->按条件查询记录的全部信息
例: select * from student where class_id=1003;--->查询班级为1003学生的信息
select * from student where name like '张%';--->查询姓张的学生信息
select 字段名,字段名2 from 表名 where 条件表达式;--->查询符合条件的记录中的某些信息
例: select name ,age from student where class_id=1001;--->查询班级为1101中的学生的姓名,年 龄
select [选项] 字段名1,字段名2 from 表名 where 条件表达式;--->查询符合条件的记录中的某些字段并对查 询结果去重.
例 select distinct name from student where class_id=1001;--->查询班级为1101中的学生的姓名 并去重
select * from 表名 order by 字段名 desc;---->以字段名降序查询表中的所有数据
例:select * from student order by id desc;---->将表中数据按ID的降序排列
select 字段名 as 别名 from 表名 where 条件表达式---->将查询出来的结果列个别名体现出来
例: select name as '姓名',age as '年龄' from student;
补充:连接查询
select * from 表一 join 表二 on 表名.字段名=表二名.字段名;--->(内连接)
例:select * from student join class on student.class_id=class.class_id;
select * from 表一 left join 表二 on 表名.字段名=表二名.字段名;---->左外连接
select * from 表一 right join 表二 on 表名.字段名=表二名.字段名;---->右外连接
select *from 表一 join 表二 using(字符段);
例: select * from student join class using(class_id);
select *from `1`left join `2` using(name) left join`3` using(class_n); 对3个表进行链 接查询
4.3 修改记录
update 表名 set 字段名=值 where 条件表达式;
例: update student set name ='周瑜' where name='周青';---->将周青的名字改为周瑜
4.4 删除记录
delete from 表名 where 条件表达式;
例: delete from student where name='周瑜';--->删除周瑜的记录
五,聚合函数
count()统计计算
select count(*) from student;--->统计student表中有多少条记录
sum()求和
select sum(age) from student;----->对表中的年龄求和
avg()求平均值
select avg(age) from student;------>对表中的年龄求平均值
max()最大值
select max(age)from student;------>查表中最大年龄是多少
min()最小值