mysql命令巧记_MYSQL常用命令大全(三)【值得收藏】
【友情提示】你必須首先登錄到MySQL中,以下操作都是在MySQL的提示符下進行的且每個命令以分號結束。
7、下面的語句在mysql環境在執行
顯示用戶擁有權限的數據庫 show databases;
切換到staffer數據庫 use staffer;
顯示當前數據庫中有權限的表 show tables;
顯示表staffer的結構 desc staffer;
8、創建測試環境
1)創建數據庫staffer
mysql> create database staffer
2)創建表staffer,department,position,depart_pos
create table s_position(
id int not null auto_increment,
name varchar(20) not null default '經理', #設定默認值
description varchar(100),
primary key PK_positon (id) #設定主鍵
);
create table department(
id int not null auto_increment,
name varchar(20) not null default '系統部', #設定默認值
description varchar(100),
primary key PK_department (id) #設定主鍵
);
create table depart_pos(
department_id int not null,
position_id int not null,
primary key PK_depart_pos (department_id,position_id) #設定復和主鍵
);
create table staffer(
id int not null auto_increment primary key, #設定主鍵
name varchar(20) not null default '無名氏', #設定默認值
department_id int not null,
position_id int not null,
unique (department_id,position_id) #設定唯一值
);
3)刪除
mysql>
drop table depart_pos;
drop table department;
drop table s_position;
drop table staffer;
drop database staffer;
9、修改結構
mysql>
#表position增加列test
alter table position add(test char(10));
#表position修改列test
alter table position modify test char(20) not null;
#表position修改列test默認值
alter table position alter test set default 'system';
#表position去掉test默認值
alter table position alter test drop default;
#表position去掉列test
alter table position drop column test;
#表depart_pos刪除主鍵
alter table depart_pos drop primary key;
#表depart_pos增加主鍵
alter table depart_pos add primary key PK_depart_pos (department_id,position_id);
10、操作數據
#插入表department
insert into department(name,description) values('系統部','系統部');
insert into department(name,description) values('公關部','公關部');
insert into department(name,description) values('客服部','客服部');
insert into department(name,description) values('財務部','財務部');
insert into department(name,description) values('測試部','測試部');
#插入表s_position
insert into s_position(name,description) values('總監','總監');
insert into s_position(name,description) values('經理','經理');
insert into s_position(name,description) values('普通員工','普通員工');
#插入表depart_pos
insert into depart_pos(department_id,position_id)
select a.id department_id,b.id postion_id
from department a,s_position b;
#插入表staffer
insert into staffer(name,department_id,position_id) values('陳達治',1,1);
insert into staffer(name,department_id,position_id) values('李文賓',1,2);
insert into staffer(name,department_id,position_id) values('馬佳',1,3);
insert into staffer(name,department_id,position_id) values('亢志強',5,1);
insert into staffer(name,department_id,position_id) values('楊玉茹',4,1);
11、查詢及刪除操作
#顯示系統部的人員和職位
select a.name,b.name department_name,c.name position_name
from staffer a,department b,s_position c
where a.department_id=b.id and a.position_id=c.id and b.name='系統部';
#顯示系統部的人數
select count(*) from staffer a,department b
where a.department_id=b.id and b.name='系統部'
#顯示各部門的人數
select count(*) cou,b.name
from staffer a,department b
where a.department_id=b.id
group by b.name;
#刪除客服部
delete from department where name='客服部';
#將財務部修改為財務一部
update department set name='財務一部' where name='財務部';
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的mysql命令巧记_MYSQL常用命令大全(三)【值得收藏】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: fastdfs笔记_fastDFS 命令
- 下一篇: 清空mysql注册表步骤_完全卸载MyS