1.數據庫的約束
show create table orders;
CREATE TABLE `orders` (
`id` int(11) NOT NULL,
`product` varchar(255) DEFAULT NULL,
`price` decimal(10,2) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
show create table student2;
`id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`sex` varchar(5) DEFAULT NULL,
`address` varchar(100) DEFAULT NULL,
`math` int(11) DEFAULT NULL,
`english` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
Select * from student2 limit 3,3;
Select product from orders group by product ;
Select product ,sum(price) as 總計 from orders group by product ;
Select product, sum(price) as 總價 from orders group by product,price;
select product ,sum(price) from orders group by product having sum(price)>30;
create table student3(id int primary key ,name varchar(20),sex varchar(20), address varchar(20),math int);
INSERT INTO student3 ( id, NAME, sex, address, math )
VALUES
( 1, "張三", "男", "北京", 80 ),
( 2, "李四", "男", "上海", 86 ),
( 3, "張潔", "男", "浙江", 90 ),
( 4, "迪麗熱巴", "女", "北京", 50 ),
( 5, "張靚穎", "女", "北京", 87 );
select sex from student3 group by sex;
select name, address from student3 where id=3;
select name from student3 where sex="男";
select sex, count(*) from student3 group by sex;
select sex, sum(math) from student3 group by sex;
select sex ,address,sum(math) from student3 group by sex,address having sum(math);
select sex ,address,sum(math) from student3 group by sex,address having sum(math)>100;
SELECT
sex,
max( math )
FROM
student3
GROUP BY
sex
HAVING
max( math );
select name,math from student3 where sex="男";
2.表關係以及多表查詢
create table user2(id int primary key auto_increment);
AUTO_INCREMENT
alter table user add id primary key;
create table user4(id int ,name varchar(20) unique);
use db1;
CREATE TABLE dept (
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20)
);
INSERT INTO dept (NAME) VALUES ('開發部'),('市場部'),('財務部');
CREATE TABLE emp (
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(10),
gender CHAR(1),
salary DOUBLE,
join_date DATE,
dept_id INT
);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('孫悟空','男',7200,'2013-02-24',1);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('豬八戒','男',3600,'2010-12-02',2);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('唐僧','男',9000,'2008-08-08',2);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('白骨精','女',5000,'2015-10-07',3);
INSERT INTO emp(NAME,gender,salary,join_date,dept_id) VALUES('蜘蛛精','女',4500,'2011-03-14',1);
select max(salary) from emp;
select name from emp where salary in (select max(salary) from emp);
select avg(salary) from emp;
select * from emp where salary >(select avg(salary) from emp);
select dept_id from emp where salary >5000;
select name from dept where id in(select dept_id from emp where salary >5000);
select id from dept where name in("開發部","財務部");
select * from emp where dept_id in (select id from dept where name in("開發部","財務部"));
select * from emp where join_date >("2011-01-01");
select e.*, dept.name from dept inner join (select * from emp where join_date >("2011-01-01")) as e on e.dept_id=dept.id;
use day0201;
create table teacher (
id int(11) not null primary key auto_increment,
name varchar(20) not null unique
);
create table student (
id int(11) not null primary key auto_increment,
name varchar(20) NOT NULL unique,
city varchar(40) NOT NULL,
age int
) ;
create table course(
id int(11) not null primary key auto_increment,
name varchar(20) not null unique,
teacher_id int(11) not null,
foreign key(teacher_id) references teacher (id)
);
create table studentcourse (
student_id int NOT NULL,
course_id int NOT NULL,
score double NOT NULL,
foreign key (student_id) references student (id),
foreign key (course_id) references course (id)
);
insert into teacher values(null,'關羽');
insert into teacher values(null,'張飛');
insert into teacher values(null,'趙雲');
insert into student values(null,'小王','北京',20);
insert into student values(null,'小李','上海',18);
insert into student values(null,'小周','北京',22);
insert into student values(null,'小劉','北京',21);
insert into student values(null,'小張','上海',22);
insert into student values(null,'小趙','北京',17);
insert into student values(null,'小蔣','上海',23);
insert into student values(null,'小韓','北京',25);
insert into student values(null,'小魏','上海',18);
insert into student values(null,'小明','廣州',20);
insert into course values(null,'語文',1);
insert into course values(null,'數學',1);
insert into course values(null,'生物',2);
insert into course values(null,'化學',2);
insert into course values(null,'物理',2);
insert into course values(null,'英語',3);
insert into studentcourse values(1,1,80);
insert into studentcourse values(1,2,90);
insert into studentcourse values(1,3,85);
insert into studentcourse values(1,4,78);
insert into studentcourse values(2,2,53);
insert into studentcourse values(2,3,77);
insert into studentcourse values(2,5,80);
insert into studentcourse values(3,1,71);
insert into studentcourse values(3,2,70);
insert into studentcourse values(3,4,80);
insert into studentcourse values(3,5,65);
insert into studentcourse values(3,6,75);
insert into studentcourse values(4,2,90);
insert into studentcourse values(4,3,80);
insert into studentcourse values(4,4,70);
insert into studentcourse values(4,6,95);
insert into studentcourse values(5,1,60);
insert into studentcourse values(5,2,70);
insert into studentcourse values(5,5,80);
insert into studentcourse values(5,6,69);
insert into studentcourse values(6,1,76);
insert into studentcourse values(6,2,88);
insert into studentcourse values(6,3,87);
insert into studentcourse values(7,4,80);
insert into studentcourse values(8,2,71);
insert into studentcourse values(8,3,58);
insert into studentcourse values(8,5,68);
insert into studentcourse values(9,2,88);
insert into studentcourse values(10,1,77);
insert into studentcourse values(10,2,76);
insert into studentcourse values(10,3,80);
insert into studentcourse values(10,4,85);
insert into studentcourse values(10,5,83);
select max(score) from studentcourse ;
select student_id from studentcourse where score in(select max(score) from studentcourse );
select * from student where id in (select student_id from studentcourse where score in(select max(score) from studentcourse ));
select max(score) from studentcourse where course_id in(1);
select student_id,score from studentcourse where course_id in(2) and score>(select max(score) from studentcourse where course_id in(1));
select e.score,student.name from student inner join (select student_id,score from studentcourse where course_id in(2) and score>(select max(score) from studentcourse where course_id in(1))) as e on e.student_id =student.id;
select * from student inner join studentcourse on studentcourse.student_id =student.id ;
select student_id, count(course_id) ,sum(score) from studentcourse group by student_id;
select student.name ,student.id, ee.* from student inner join (select student_id, count(course_id) ,sum(score) from studentcourse group by student_id) as ee on ee.student_id =student.id;