1 数据库操作语句(增删改数据)
1.插入数据
INSERT INTO table_name (列名) VALUES (值);
[如果不写列名,values后面必须给表中的所有列赋值,并且和列一一对应]
eg: insert into user(id,name,age) values(1,'zhangsan',20);
insert into user values(1,'zhangsan','男',20);
2.修改数据
UPDATE table_name SET col_name1 = 值 ,col_name2 = 值2
[WHERE where_definition]
eg:update user set name ='zhaoliu' where id = 1 ;
3.删除数据
delete from table_name
[WHERE where_definition]
eg: delete from user where id = 1;
查询数据
1.查询所有数据库
SELECT * from table_name;
eg:select * from user;
2.查询指定列数据
SELECT 列名,列名,列名 from table_name;
eg:select id, name from user;
3.查询时指定常量列(别名)
SELECT 列名 as 别名 from table_name;
as 可以不写
eg: mysql> select id, name as 姓名 from user;
mysql> select id, name 姓名 from user;
4.查询时去除重复数据 DISTINCT
eg: select distinct id,name from user;
5.查询时合并(这里合并的是数值)
eg: select id,name,age,java+math from user;
select id,name,age,java+math as total from user;
6.按条件查询 WHERE
eg: select * from user where id =2;
a 逻辑条件查询,涉及多个条件 and &&(与) or(或)
eg: select * from user where id =2 and name = 'lisi';
select * from user where name ='lisi' or age = 22;
b 比较条件查询 > 、 >= 、< 、<= 、= 、<> , between and(谁和谁之间)
eg: select * from user where id<>2;
eg: select * from user where id>2;
eg: select * from user where id>=2;
eg: select * from user where id between 2 and 5;
c 判断是否为null(null表示没有数据)
mysql> select * from user where age is null;
mysql> select * from user where age is not null;
d 模糊查询 like
%通配符,可以匹配多个字符
_通配符,匹配一个字符
mysql> select * from user where name like 'zhao%';
mysql> select * from user where name like '%s%';
mysql> select * from user where name like 'zhaos_';
7 通过聚合函数查询
a计算表中有多少条数据(行)count
eg: select count(*) from user;
如果某些行对应的列没有数据,不计算到总数里
eg: select count(age) from user;
b求平均值 avg
eg:select avg(age) from user;
c求最大值和最小值
eg: select min(age) from user;
eg: select max(age) from user;
d求和 sum
mysql> select sum(age) from user;
8.排序 order by
eg: select * from user order by age;
eg: select * from user where age is not null order by age;
desc表示降序,asc表示升序,默认升序
eg: select * from user where age is not null order by age desc;
可以指定多个排序的字段,例如先按照age排序,相同的age值再使用id排序
eg:select * from user where age is not null order by age,id;
eg:select * from user where age is not null order by age desc,id desc;
9 分组查询 group by(也可以实现去重操作)
eg: select gender, count(*) from user group by gender;
eg:select gender,count(*) from stu group by gender order by gender desc;
having 对分组后的数据进行筛查
where 放在分组前使用
eg:select gender,count(*) from user where age is not null group by gender having count(*)>1 order by gender desc;
10 分页查询 limit
Limit 从哪个位置开始查(索引从0开始计数), 查询多少条数据
eg:select * from user limit 3,3;
表示从0位置开始查询3条数据
eg:select * from user limit 3;
2 数据库高级使用
1 数据约束
约束(Constraint)是SQL 提供的自动保持数据库完整性的一种方法,定义了可输入表或表的单个列中的数据的限制条件。
1.默认值 default
表中的字段,默认的默认值为NULL
如果插入数据时,不给某个字段赋值,那么这个字段就使用设置的默认值。如果赋值,就使用实际赋的值,这个值可以为NULL
创建时设置某个字段的默认值
create table student(
id int default 100,
name char(10) default 'zzz'
)
修改某个字段的默认值
alter table student modify column name char(10) default 'smile';
MYSQL里可以使用FIRST和AFTER关键字指定添加的字段的位置
2.非空 (是否为null)
插入数据时,如果非空字段赋值为NULL,运行sql命令会报错。如果非空字段没有赋值,会使用该字段的默认值
修改
alter table student modify java int not null;
创建时设置某个字段非空
CREATE table user(
id int not null,
name varchar(10) not null,
age int ,
java int
);
3.唯一 (Unique)
如果某个字段设置为unique,在整个表中,该字段的值不能重复。但是,设置为unique的字段,值可以为NULL,而且NULL可以重复出现
修改
alter table student modify score int unique;
eg: insert into student(id,score) values(8,100);
//运行多次,报错
1062 - Duplicate entry '100' for key 'score'
eg: insert into student(id,score) values(8,NULL); //运行多次,不会报错
4.自增(AUTO INCREMENT)
ALTER TABLE student
MODIFY COLUMN id int(11) AUTO_INCREMENT FIRST ;
某个字段 单独设置为自增,报错,必须结合主键使用
设为自增的字段,值从1开始,每次插入数据时,找到该字段的最大值,然后进行+1的自增操作
插入数据时,如果指定字段,针对自增字段,可以赋值,也可以不赋值。但是,如果表名后没有指定字段,表示每个字段都要赋值,自增字段也必须赋值
5.主键 (PRIMARY KEY)
通过主键,可以保证数据表中的记录唯一
一般将表示唯一数据的字段,设为主键,如学号、身份证号、自增字段等
添加主键
alter table student add PRIMARY KEY(id);
【在添加主键之前,必须先把重复的id删除掉。】
删除主键
alter table student drop primary key;
可以通过多个字段表示联合主键
create table seckill(
phone varchar(11) not null,
name varchar(11),
p_num int,
p_name varchar(12),
p_price double,
primary key(phone,p_num)
)
主键相当于:非空(not null) + 唯一(unique)
6.外键 (FOREIGN KEY)
学生信息 student 【id,name,age,tid】
tid 就是student表中的外键
对应的是teacher表中id
老师信息 teacher
【id,name,phone】
外关键字约束定义了表之间的关系。当一个表中的一个列或多个列的组合和其它表中的主关键字定义相同时,就可以将这些列或列的组合定义为外关键字,并设定它适合哪个表中哪些列相关联。这样,当在定义主关键字约束的表中更新列值,时其它表中有与之相关联的外关键字约束的表中的外关键字列也将被相应地做相同的更新。外关键字约束的作用还体现在,当向含有外关键字的表插入数据时,如果与之相关联的表的列中无与插入的外关键字列值相同的值时,系统会拒绝插入数据。与主关键字相同,不能使用一个定义为 TEXT 或IMAGE 数据类型的列创建外关键字。外关键字最多由16 个列组成。
建立外键
学生表 (id,name,score,tid)
教师表 (id,name,phone,salary)
alter table student add
foreign key (tid)
references teacher (id);
通过外键对多个表的数据进行约束
student表中的tid为外键,关联teacher表的id字段
主表:通过其他表的外键进行关联,约束其他表的数据,比如teacher表
副表:有外键的表,被约束的表,比如student表
增加外键后:
向student表中添加数据时,如果对应的teacher字段的值,在teacher表中没有,插入数据失败
student 外键 副表在添加
teacher 主表
进行数据操作时,要求:
添加数据:主表先添加数据,副表再添加
删除数据:先删除副表数据,再删除主表
更新数据:主副表更新都会失败
1451 - Cannot delete or update a parent row: a foreign key constraint fails
但是如果想删除或更新主表数据不报错,需要进行级联操作
(2)级联操作
Cascade
删除主表数据时,也删除副表数据
更新主表数据时,也更新副表数据
alter table student add
CONSTRAINT `fid1`
foreign key (tid)
references teacher (id)
on delete CASCADE
on UPDATE CASCADE;
【
on update 和 on delete 后面可以跟的词语有四个
no action , set null , set default ,cascade
no action 表示不做任何操作,
set null 表示在外键表中将相应字段设置为null
set default 表示设置为默认值
cascade 表示级联操作,就是说,如果主键表中被参考字段更新,外键表中也更新,主键表中的记录被删除,外键表中改行也相应删除
】
Restrict 表示没有级联操作 同 no action