SQL(数据操作语⾔)DML命令应用

一、给表插入数据

1. insert

insert into department (dno, dname, dloc) values (10, '财务部', '北京');
insert into department (dno, dname, dloc) values
(20, '研发1部', '成都'),
(30, '销售1部', '上海'),
(40, '研发2部', '深圳'),
(50, '销售2部', '长沙');

2. delete

delete from department where dno = 50;
delete from department where dno in (30, 40);

3. update

update department set dname = '销售3部', dloc = '武汉' where dno = 30;

4. select

select * from department;

4.多对多写法

create table tb_user
(
uid int unsigned not null auto_increment,
nickname varchar(20) not null,
reg_date datetime not null,
vip_level int not null default 1,
primary key (uid)
);

create table tb_bike
(
bid int not null auto_increment,
in_use boolean not null default 0,
is_broken boolean not null default 0,
primary key (bid)
);

create table tb_record
(
rid bigint unsigned not null auto_increment comment '流水号',
uid int unsigned not null comment '用户ID',
bid int not null comment '自行车ID',
start_time datetime not null comment '开始时间',
end_time datetime comment '结束时间',
pay_way varchar(10) comment '支付方式',
payment decimal(5,2) comment '骑行费用',
primary key (rid),
foreign key (uid) references tb_user (uid),
foreign key (bid) references tb_bike (bid)
);

5.选择输出

select stu_name as 姓名,
	case stu_sex when 1 then'男' else'女' end as'未知' end as 性别,
	stu_birth as 出生日期
from tb_student
where stu_birth between '1980-1-1' and '1989-12-31';

6.通配符
% 表示匹配0个或者任意多个字符
_ 表示匹配1个任意字符
union 两个条件求并集
union all 两个条件求并集 但是不去掉偿付元素

7.总体思路
select … as …
, distinct … as …
from …
natural join …
inner join … on …
left / right outer join … on …
where … and … or …
group by …, …
with rollup
having …
order by … asc, … desc
limit … offset …

你可能感兴趣的:(关系型数据库SQL,sql,数据库,java)