MySql基础知识

1、MySQL下载
https://dev.mysql.com/downloads
安装时根据需要进行。
2、数据库的创建和删除
CREATE DATABASE sql_tutorial
SHOW DATABASE
DROP DATABASE sql_tutorial
USE sql_tutorial
3、数据表的创建
创建
CREATE TABLE student(
student_id INT PRIMARY KEY,
name VARCHAR(20),
major VARCHAR(20)
);

CREATE TABLE student(
student_id INT ,
name VARCHAR(20),
major VARCHAR(20),
PRIMARY KEY(·student_id·)
);
显示
DESCRIBE student
删除
DROP TABLE student
增加列
ALTER TABLE student ADD gpa DECIMAL(3,2);
删除列
ALTER TABLE student DROP COLUMN gpa;
4、数据操作
添加数据
INSERT INTO student VALUES(1,“小白”,“历史”);
insert into student(name,major,student_id) values(“小黑”, ‘地理’,2);
insert into student(name,major,student_id) values(“小二”, null,3);
查询数据
SELECT * FROM student
5、案例
create table employee(
emp_id int primary key,
name varchar(20),
birth_date date,
sex varchar(1),
salary int,
branch_id int,
sup_id int
);
describe employee;

create table branch(
branch_id int primary key,
branch_name varchar(20),
manager_id int,
foreign key(manager_id) references employee(emp_id) on delete set null
);
describe branch;

alter table employee add foreign key(branch_id)
references branch(branch_id) on delete set null;

alter table employee add foreign key(sup_id)
references employee(emp_id) on delete set null;

create table client(
client_id int primary key,
client_name varchar(20),
phone varchar(20)
);

create table work_with(
emp_id int,
client_id int,
total_sales int,
primary key(emp_id, client_id),
foreign key(emp_id) references employee(emp_id) on delete cascade,
foreign key(client_id) references client(client_id) on delete cascade
);

insert into branch values(1,‘研发’,null);
insert into branch values(2,‘行政’,null);
insert into branch values(3,‘资信’,null);
select * from branch;

insert into employee values(206,‘小黄’,‘2000-10-08’,‘F’,50000,1,null);
insert into employee values(207,‘小红’,‘2001-10-08’,‘M’,29000,2,206);
insert into employee values(208,‘小程’,‘2002-10-08’,‘M’,35000,3,206);
insert into employee values(209,‘小绿’,‘2003-10-08’,‘F’,39000,3,207);
insert into employee values(210,‘小青’,‘2004-10-08’,‘F’,84000,1,207);
select * from employee;

update branch set manager_id = 206 where branch_id =1;
update branch set manager_id = 207 where branch_id =2;
update branch set manager_id = 208 where branch_id =3;

insert into client values(400,‘阿狗’,‘22334455’);
insert into client values(401,‘阿猫’,‘22334456’);
insert into client values(402,‘旺来’,‘22334457’);
insert into client values(403,‘露西’,‘22334458’);
insert into client values(404,‘艾瑞克’,‘22334459’);
select * from client;

insert into work_with values(206,400,‘70000’);
insert into work_with values(207,401,‘24000’);
insert into work_with values(208,402,‘9800’);
insert into work_with values(208,403,‘24000’);
insert into work_with values(210,404,‘87940’);
select * from work_with;

select * from employee order by salary;
select * from employee order by salary limit 3;
select * from employee order by salary desc;
select distinct sex from employee;
select count() from employee;
select count(sup_id) from employee;
select count(
) from employee where birth_date > ‘2002-10-08’ and sex = ‘F’;
select avg(salary) from employee;
select sum(salary) from employee;
select max(salary) from employee;
select min(salary) from employee;

select * from client where phone like ‘223%’;
select * from employee where birth_date like ‘___2%’;

select name from employee union select client_name from client;
select emp_id, name from employee union select client_id, client_name from client;
select emp_id as total_id, name from employee union select client_id, client_name from client;
select salary from employee union select total_sales from work_with;

select * from employee join branch on emp_id = manager_id;
select emp_id, name, branch_name from employee join branch on employee.emp_id = manager_id;
select emp_id, name, branch_name from employee left join branch on employee.emp_id = manager_id;
select emp_id, name, branch_name from employee right join branch on employee.emp_id = manager_id;
select name from employee where emp_id = (
select manager_id from branch where branch_name = ‘研发’
);
select name from employee where emp_id in (
select emp_id from work_with where total_sales >50000
);

delete from employee where emp_id = 207;

你可能感兴趣的:(mysql,数据库)