将表t4的第三条记录姓名字段改为杨康update t4 set name='杨康' where id = 3 ;
将所有记录的名字都改为东方不败 update t4 set name = '东方不败' ;
修改多个字段 update t4 set id=6,name='萧峰' where id = 2 ;
删除
delete from t4 where id = 4 ;
删除所有的记录 delete from t4 ;
删除所有的记录 truncate table t4 ;
给表t4增加一个字段address alter table t4 add address varchar(100) ;
删除字段address alter table t4 drop column address ;
查看表的结构 desc t4 ;
创建一个学生表
create table stu ( id int primary key, // 主键约束 name varchar(30) unique, // 唯一约束 sex char(2) not null, //非空约束 age int check (age > 0 and age < 100),// 检查约束 address varchar(50) default '北京' //默认约束 ) ;
insert into stu values(1,'张无忌','男',20,'北京') ; insert into stu values(2,'小龙女','女',18,'古墓') ; insert into stu values(3,'黄蓉','女',15,'桃花岛') ; insert into stu values(4,'韦小宝','男',24,'扬州') ; insert into stu values(5,'乔峰','男',34,'雁门关') ; insert into stu values(6,'张果老','男',30,'雁门关') ; insert into stu values(7,'老张','男',38,'黒木崖') ; insert into stu values(8,'张','男',34,'桃花岛') ; insert into stu values(9,'韦小宝','女',24,'新东方') ; insert into stu(id,name,sex,age) values(10,'令狐冲','男',27) ;
查看所有数据 select * from stu ;
查看小龙女的信息 select * from stu where id = 2 ; select * from stu where name='小龙女' ;
查看年龄在20~30之间的人 select * from stu where age >=20 and age <=30 ; select * from stu where age between 20 and 30 ; # 包括20和30 #查看所有的的姓名 select name from stu ;
查看所有的的姓名,年龄,性别 select name,age,sex from stu ;
模糊查询
select * from 表名 where 字段名 like 字段表达式 % 表示任意字符数 _ 表示任意的一个字符 [] 表示在某个区间
查询所有以张开头的人 select * from stu where name like '张%' ;
查询姓名中含有张这个字的人 select * from stu where name like '%张%' ;
查询姓名中含有张这个字的人并且姓名的长度是3个字的人select * from stu where name like '张__' or name like '_张_' or name like '__张' ;
查询表中有几种性别 select distinct sex from stu ;
查找姓名和性别整体都不同的记录 select distinct name,sex from stu ;
创建新表分数表
create table score ( id int primary key, sid int , china int, english int , history int, constraint sid_FK foreign key(sid) references stu(id) ) ;
insert into score values(1,1,68,54,81) ;
insert into score values(2,3,89,98,90) ;
insert into score values(3,4,25,60,38) ;
insert into score values(4,6,70,75,59) ;
insert into score values(5,8,60,65,80) ;
字段可以有表达式 select id,china+10,english,history from score ;
给字段起别名select id as 编号,china as 语文,english as 英语,history as 历史 from score ; select id 编号,china 语文,english 英语,history 历史 from score ;
查看所有人考试的总分是多少 select id,china + english + history 总分 from score ;
查看总分大于200的人 select * from score where china + english + history > 200 ;
查询家在桃花岛或者黒木崖的人
select * from stu where address = '桃花岛' or address = '黒木崖' ;select * from stu where address in('桃花岛','黒木崖') ;
查询没有参加考试的人 select id ,name from stu where id not in(select sid from score) ;
查询没有地址的人 select * from stu where address = null ; #错误的 select * from stu where address is null ;
查询有地址的人 select * from stu where address is not null ;
排序(order by )
对考试的人的语文升序asc排列 select * from score order by china asc;
对考试的人的历史降序desc排列 select * from score order by history desc;
根据多个字段进行排序(语文升序,对语文成绩一样的人再进行历史降序类排)select * from score order by china asc,history desc;
根据考试总分降序进行排序 select *,china + english + history 总分 from score order by china + english + history desc ;主键:唯一的去区分每一条记录的一列或者多列的值. 特点:唯一,非空
Order by指定排序的列,排序的列即可是表中的列名,也可以是select 语句后指定的列名。
Asc升序、Desc降序
ORDER BY 子句应位于SELECT语句的结尾。
域完整性:
指数据库表的列(即字段)必须符合某种特定的数据类型或约束。比如NOT NULL。主键primary key 唯一约束unique 检查约束 MySQL不支持哦int check (age > 0 and age < 100) 非空约束not null
create table stu ( id int primary key, // 主键约束 name varchar(30) unique, // 唯一约束 sex char(2) not null, //非空约束 age int check (age > 0 and age < 100),// 检查约束 address varchar(50) default '北京' //默认约束 ) ;
参照完整性:
保证一个表的外键和另一个表的主键对应。
7、查询
连接查询
交叉连接(cross join):不带on子句,返回连接表中所有数据行的笛卡儿积。
内连接(inner join):返回连接表中符合连接条件及查询条件的数据行。
外连接:分为左外连接(left out join)、右外连接(right outer join)。与内连接不同的是,外连接不仅返回连接表中符合连接条件及查询条件的数据行,也返回左表(左外连接时)或右表(右外连接时)中仅符合查询条件但不符合连接条件的数据行。
子查询
子查询也叫嵌套查询,是指在select子句或者where子句中又嵌入select查询语句 查询“陈冠希”的所有订单信息
SELECT * FROM orders WHERE customer_id=(SELECT id FROM customer WHERE name LIKE '%陈冠希%');
联合查询
联合查询能够合并两条查询语句的查询结果,去掉其中的重复数据行,然后返回没有重复数据行的查询结果。联合查询使用union关键字
SELECT * FROM orders WHERE price>200 UNION SELECT * FROM orders WHERE customer_id=1;
报表查询
报表查询对数据行进行分组统计,其语法格式为:
[select …] from … [where…] [ group by … [having… ]] [ order by … ]
其中group by 子句指定按照哪些字段分组,having子句设定分组查询条件。在报表查询中可以使用SQL函数。