-- 创建poduction产品表
create table poduction(
pid int not null auto_increment,-- id
pno int null,-- 产品编号
pname varchar(50) null,-- 产品名称
pdate date null,-- 生产日期
primary key(pid)-- 设置pid列主键自增
);
commit;
查询select * from poduction;
插入
insert into poduction(pno,pname,pdate) values(1,'产品A','2018-11-12'), (2,'产品B','2017-05-25'),(3,'产品C','2016-10-06');
commit;
修改update student set sage=19 where sage=10; --将sage=10修改为sage=19
删除 delect from student where id=3;
--添加新列alter table t_name add 新列 varchar(10)
--删除mgr列 alter table t_name drop column mgr;
--更改列的数据类型alter table t_name alter column 新列 int not null
静态行转为列:
-- 后面的这个新语文是新建的列的列名,就是字段名
select s.sname,
max(case s.subjects when '语文' then s.scores else 0 end ) '新语文',
max(case s.subjects when '数学' then s.scores else 0 end ) '新数学',
max(case s.subjects when '英语' then s.scores else 0 end ) '新英语'
from student2 s
group by s.sname
--内连接inner join
select stu1.stuid,sco1.courseid from scores sco1 inner join student stu1
on stu1.stuid=sco1.stuid;
--查询名称包含BBB的代理商的所有销售人员?
select t1.aname , s.sname from (
select * from agent a where a.ano=2)t1
inner join sales s on t1.ano = s.ano ;
--左外连接left join、右外连接right join
select stu1.stuid,sco1.courseid from scores sco1 left join student stu1
on stu1.stuid=sco1.stuid;
--模糊查询,count数,查询姓欧阳的老师的个数?
select count(*) from teacher where teachername like '欧阳%'
Where in的用法
1、Select * from student s where s.sid in(3); --括号里面也可能是一个数组
2、select * from poduction where pno in(
select t1.pno2 from (
select pno as pno2,sum(volume) as sum2 from deal group by pno order by sum2 desc limit 2) t1
)
-- where条件 查询,and和,or或,三科都及格的人的数据
select * from stutest where chinese>=60 and math>=60 and english>=60;
升序降序:
select * from emp order by empid desc; //asc是升序,默认是升序
Oracle数据库插入,需要先创建序列
-- 创建序列(创建名为p_pro的序列,该序列从10开始以1的增速自增)
create sequence p_pro start with 10 increment by 1;
插入insert into products values (p_pro.nextval,1,'iPhone手机',8000,2000,'img/1.png',150,101,1);--须借助序列来插入数据
如果两个数据库在同一台服务器上:
1、复制表结构和数据:select * into databaseB.dbo.tableB from databaseA.dbo.tableA
2、仅仅复制表结构:select * into databaseB.dbo.tableB from databaseA.dbo.tableA where 1=2;
清空数据truncate table st_table:
truncate table 与 delete table 的区别是:truncate tabnle 不仅是删除表里面的数据,而且还会清空表里面主键的标识。也就是说使用过truncate table的表在重新写入数据的时候,标识符会从0或1重新开始(看你设置的种子号)。delete table 仅仅能删除数据,不能请空标识。
-- 查询每个人出现了几次;group by 分组和count次数查询;
select s.sname,count(s.sname) from stest1 s group by s.sname;
-- 查询水表两个月以上包括两个月未交水费的水表和未交次数(group by分组 ...having 条件查询)(where条件查询用在group by前面,having条件查询用在group by 后面而且多和聚合函数一起使用,如例子中count()。)
-- select s.sname from shui s where s.sstatu=0 group by s.sname having count(s.sname)>1;--没有次数
select s.sname,count(s.sname)as 次数 from shui s where s.sstatu=0 group by s.sname having count(s.sname)>1;--有次数
-- 查询这个班级有多少个人;distinct是去重的作用
select count(distinct s.sname) as 总人数 from stest1 s;
-- 查询出男女各多少人
select s.ssex ,count(distinct s.sname) from stest1 s group by s.ssex;
–直接创建索引
CREATE INDEX index_name ON table(column(length))
–修改表结构的方式添加索引
ALTER TABLE table_name ADD INDEX index_name ON (column(length))
如:ALTER TABLE article ADD INDEX index_article_title ON title(200);
唯一索引与普通索引类似,不同的就是:索引列的值必须唯一,但允许有空值(注意和主键不同)。如果是组合索引,则列值的组合必须唯一,创建方法和普通索引类似。
–创建唯一索引
CREATE UNIQUE INDEX indexName ON table(column(length))
–修改表结构的创建唯一索引
ALTER TABLE table_name ADD UNIQUE indexName ON (column(length))
--删除索引
DROP INDEX index_name ON table