赞
踩
数据库和表的操作
1.创建数据库
create database+库名
1)create database mydb1; utf-8(默认此字符)
2)使用gbk语句创建数据库
create database wrg character set gbk;
3)使用utf-8,并带校对规则(排序)的数据库 语法默认为utf8
create database wrg character set utf8 collate utf8_bin
2.查看数据库
1)show database;
2)显示数据库创建语句
show create database+库名;
3.修改数据库
alter database +库名
1) 修改数据库语句
alter database wrg character set+库句;
4.删除数据库
drop database +库名;
5.选择数据库
use database+库名;
查看所选择的数据库
select database();
数据库中表的操作
mysql 数据库中的数据类型
1.整形数据,足够使用情况下使用尽量小的
tinyint
smallint
int
bigint
2.浮点数
float
double
decimal(整数,小数)【自己定义】
3.日期时间
year 0000代表年
date 0000-00-00 yyy-mm-dd
time 00:00:00
datetime yyyy-mm-dd hh-mm-ss
timestamp
4.字符串
char
varchar
binary varbinary
text大文本类型
blob存电影
enun枚举类型
set字符串对象
bit(m)m【01】表示数据类型
1)增加表
create table +表名
create table wrg(
-> id int,
-> name varchar(20),
-> gender char(1),
-> birth date,
-> job varchar(50),
-> salay double,
-> resume text);
2)查看表
show tables;
看表结构desc wrg;
查看建表语句show create table +表名
表的增删改查
3)修改表
增加 alter table +表名+add image blob;
修改 alter table 表名 modify job varchar(60);
删除 alter table 表名 drop+列名;
修改表名 rename table (旧表名)wrg to (新表名)rg;
修改表的字符集编码alter table 表名 character set utf8;
修改列名 alter table 表名 change 列名 新列名 varchar(8);
4)删除表
drop table 表名
约束
约束
primary key(不重复)
foreign key
not null
default
unique
1)多字段约束
create table wrg(
dh id int,
xh id int,
primary key(sj_id,xh_id));
2)非空约束
not null写在最后
create table wrg(
dh id int not null,
xh id int,
primary key(sj_id,xh_i
3)唯一约束
unique写在最后面
create table wrg(
-> id int,
-> name varchar(20) unique,
-> gender char(1),
-> birth date,
-> job varchar(50),
-> salay double,
-> resume text);
名字不重复
4)默认约束
default '默认值'
create table wrg(
-> id int,
-> name varchar(20) unique,
-> gender char(1),
-> birth date,
-> job varchar(50) default‘实习生’,
-> salay double,
-> resume text);
索引
1)概念:对数据库表中一列或多列的值进行排序的一种结构,不用挨条找数据,提高查询速度
2)普通索引:可以创建在任何数据类型中,没有固定要求
创建索引;create table 表1(
id int,
name varchar(20),
score float,
index(id));
查询字段:explain select * from t1 where id=1;
2)唯一索引:用unique定义索引,该索引所在字段的值必须是唯一的
创建:create table t2(
id int,
name varchar(20),
score float,
unique index unique_id【别名】(id asc));
在已经存在的表中创建索引:
create unique index wysy on teacher(id);
查询字段:explain select * from t1 where id;
3)全文索引:fulltext只能创建字符串类型字段上,引擎只能是Myisam
创建;mysql> create table t3(
-> id int not null,
-> name varchar(20) not null,
-> sorce float,
-> fulltext index fulltext_name(name))engine=myisam;
create fulltext index on teachar(name);【先把之前表的引擎改为myisam】
4)单列索引:创建在表中的一个字段上,保证该索引只对应表中一个字段
create table t4(
id int,
name varchar(20),
score float,
index wrg_name【别名,可自定义】(name));
5)多列索引:创建在多个字段,只有在查询条件中使用了这些字段的第一个字段中,该索引才会被使用
mysql> create table t52(
-> id int,
-> name varchar(20),
-> index wrg(id,name(20)));
explain select * from t1 where id=1;
create index duolie on teacher(id,name);
6)空间索引:由spatial定义的索引,只能创建在空间数据类型上(geometry,point,linestring,polygon),只能在储存引擎为MyISAM的表中创建
mysql> create table t6(
-> id int,
-> space geometry not null,
-> spatial index sp(space)
-> )engine=myisam;
create spatial index on teachar(name);【先把之前表的引擎改为myisam】
创建索引语法:
查看索引: show index from t1;
1.创建表的时候创建索引
create table 表名(字段名 数据类型[完整性约束条件],
字段名 数据类型[完整性约束条件],
[可选参数unique,fulltext,spatial](字段名[(长度)][排序asc|desc]));
在已经存在的表上创建索引:
create[unique|fulltext|spatial] index 索引名 on 表名 (字段名[(长度)] [asc|desc]);
使用alter语句在已存在的表上创建索引
alter table表名 add [参数]index 索引名 (字段名(长度)[asc|desc]);
例:alter table teacher add index wrg (id(4)[asc]);
7)删除索引
1.alter table book drop index 索引名
2.drop index 索引名 on 表名;
第三章 操作表记录(增,删,改,查;create read,update,Delete)
修改字段名
alter table 表名 change 旧字段名 新字段名,新数据类型,不可写主键;
1)插入数据:
insert into table values("","","");
2)更新数据:
Update 表名 set 字段名=数值 where id='66';条件
3)删除数据
delete from 表名 where=“”;条件
truncate (摧毁表,在创建相同表结构)
truncate 表名;
第四章 单表查询
1)select distinct * from 表名;不重复查询
2)select 字段+数值 from 表名;
3)where语句查询:
select * from cj where cj>80;
4)查询分数在80~90的成绩:
select * from cj where cj between 80 and 90;
5)查询姓王的学生信息
select * from xs where xm like '王%';
“%”指的是王零个或多个字符 “_”指的是零个或一个字符
聚合函数
1)count用来统计行数
select count(*) from cj where cj>90;
2)sum用来统计和
select sum(cj) from cj where kch='206';
3)avg统计平均分
select avg(cj) from cj where kch='206';
4)max/min统计最高和最低
select min(cj) from cj;
select max(cj) from cj;
查询结果排序
desc升序asc降序,order by 指定排序列名,应放最结尾部分
1) select cj from cj order by kch='206' desc;
2)对姓王的成绩进行排序
select cj from cj where xm like '王' order by kch=‘206’ desc;
分组查询
group by 按照某一列或者,分组之前用where过滤,分组之后用having过滤,where语句中不允许使用聚合函数
1) select * from 表名 group by 列名;
查询课程编号列的成绩大于80分的信息
select * from cj group by kcbh having cj>80;
查询课程编号列成绩大于80分小于等于80分的信息
select * from cj where cj<=85 group by kcbh having cj>80;
使用limit限制查询结果数量
1)查询前5条信息
mysql> select * from cj limit 5;
2) 查询第3条到第5条数据
mysql> select * from cj limit 2,3;【2指的是从第三条开始,3指的是从第三条开始之后的三条数据】
为表和字段取别名
1)为表名取别名
select * from 表名 as 别名;
为字段名取别名
select * 字段名 as 别名 from 表名;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。