mysql -uroot -p密碼
show databases;--檢視所有資料庫
use db1; -- 使用其中乙個資料庫
create table dept(
deptno int primary key,
dname varchar(14), -- 部門名稱
loc varchar(13)-- 部門位址
) ;
insert into dept values (10,'accounting','new york');
insert into dept values (20,'research','dallas');
insert into dept values (30,'sales','chicago');
insert into dept values (40,'operations','boston');
select * from dept;
drop table emp;
create table emp(
empno int primary key, -- 員工編號
ename varchar(10), -- 員工姓名
job varchar(9), -- 員工工作
mgr int, -- 員工直屬領導編號
hiredate date, -- 入職時間
sal double, -- 工資
comm double, -- 獎金
deptno int, -- 所在部門
foreign key (deptno) references dept(deptno)); -- 關聯dept表
-- alter table emp add foreign key (deptno) references dept(deptno);
insert into emp values(7369,'smith','職員',7566,"1980-12-17",800,null,20);
insert into emp values(7499,'allen','銷售員',7698,'1981-02-20',1600,300,30);
insert into emp values(7521,'ward','銷售員',7698,'1981-02-22',1250,500,30);
insert into emp values(7566,'jones','經理',7839,'1981-04-02',2975,null,20);
insert into emp values(7654,'martin','銷售員',7698,'1981-09-28',1250,1400,30);
insert into emp values(7698,'blake','經理',7839,'1981-05-01',2850,null,30);
insert into emp values(7782,'clark','經理',7839,'1981-06-09',2450,null,10);
insert into emp values(7788,'scott','職員',7566,'1987-07-03',3000,2000,20);
insert into emp values(7839,'king','董事長',null,'1981-11-17',5000,null,10);
insert into emp values(7844,'turners','銷售員',7698,'1981-09-08',1500,50,30);
insert into emp values(7876,'adams','職員',7566,'1987-07-13',1100,null,20);
insert into emp values(7900,'james','職員',7698,'1981-12-03',1250,null,30);
insert into emp values(7902,'ford','銷售員',7566,'1981-12-03',3000,null,20);
insert into emp values(7934,'miller','職員',7782,'1981-01-23',1300,null,10);
select * from emp;
create table salgrade(
grade int,-- 等級
losal double, -- 最低工資
hisal double ); -- 最高工資
insert into salgrade values (1,500,1000);
insert into salgrade values (2,1001,1500);
insert into salgrade values (3,1501,2000);
insert into salgrade values (4,2001,3000);
insert into salgrade values (5,3001,9999);
select * from salgrade;
select * from
empwhere
deptno=30;
select
empno,
ename,
deptno
from
empwhere
job='職員';
select * from
empwhere
comm>sal;
select * from
empwhere
comm>sal*0.6;
select * from
empwhere
ename like '%a%';
select * from
empwhere
ename like 'a%'
or ename like 'b%'
or ename like 's%';
select * from
empwhere
ename like'_______';
select * from
empwhere
ename
not like
'%r%';
select * from
emporder by
ename;
select * from
emporder by
ename desc;
sal asc;
select
ename,
sal/30
from
emp;
select * from
empwhere
comm>0;
select * from
empwhere
comm>0
or comm is null;
select * from
emp
where
deptno=10
and
job='經理'
or (deptno=20 and job='職員');
select * from
empwhere
deptno=10
and job='經理'
or deptno=20
and job='職員'
or job not in('經理','職員')
and sal>2000;
MySQL之單錶查詢練習
一 emp表 二 練習 1.查詢出部門編號為30的所有員工 2.所有銷售員的姓名 編號和部門編號。3.找出獎金高於工資的員工。4.找出獎金高於工資60 的員工。5.找出部門編號為10中所有經理,和部門編號為20中所有銷售員的詳細資料。6.找出部門編號為10中所有經理,部門編號為20中所有銷售員,還有...
mysql總結3 單錶查詢1
crud 增刪改查。create 增加 retrieve 查詢 update 更新 delete 刪除 1.插入一行 比如對t user表插入一行資料,我們可以這樣寫 insert into t user username,password,age,address values tom 123 12...
資料庫Mysql 2 建立表 刪表
1.建立表 create table table name 列名1 列名屬性,列名2 列名屬性,列名3 列名屬性 2.刪除表 drop table table name 當你不需要該錶時,可以用drop 也就是說包括表名和資料啥都沒有了 當你仍要保留該錶,但要刪除所有記錄時,用 truncate 仍...