經典SQL語句
create database information;
#建立部門資料(主表):bumen(有編號,姓名),其中Id為主鍵並且自動增長
create table bumen(id int primary key auto_increment,name varchar(10));
#向表中插入以下資料
insert into bumen(name) values ('總裁辦');
insert into bumen(name) values ('人事部');
insert into bumen(name) values ('後勤部');
insert into bumen(name) values ('財務部');
insert into bumen(name) values ('教學部');
#建立員工資料(從表):yuangong,資料型別:id 整數型name 字串sex 字串age 整數型address 字串
create table yuangong(name varchar(10),sex varchar(10),age int,id int,address varchar(10));
#向表中插入以下資料
insert into yuangong(name,sex,age,id)values('王小二','男',20,1);
insert into yuangong(name,sex,age,id)values('張三','男',40,1);
insert into yuangong(name,sex,age,id)values('李四','女',38,5);
insert into yuangong(name,sex,age,id)values('王五','男',25,4);
insert into yuangong(name,sex,age,id)values('趙六','女',26,3);
insert into yuangong(name,sex,age,id)values('錢七','女',20,3);
insert into yuangong(name,sex,age,id)values('馮八','男',20,4);
#為從表新增外來鍵
alter table yuangong add foreign key(id) references bumen(id);
select *from bumen;
select *from yuangong;
#查詢"財務部"所有"40歲以下男員工"的"所有資訊"
select *from yuangong y,bumen b where y.id=b.id and y.age<40 and y.sex='男' and b.name='財務部';
#將張三的的名字改為李四,並調到財務部。
update bumen set name='財務部' where id=(select id from yuangong where name='李三');
update yuangong set name='李四' where name='李三';
#將後勤部年齡大於10歲的員工刪除。
delete from yuangong where age>10 and id=(select id from bumen where name='後勤部');
#查詢"財務部""年齡不在20-30之間"的"男生"資訊
select *from yuangong y,bumen b where y.id=b.id and y.age not between 20 and 30 and y.sex='男' and b.name='財務部';