在安裝在mysql之后,還沒有用可視化的軟件使用數(shù)據(jù)庫,我們只能使用cmd 終端 來創(chuàng)建并使用數(shù)據(jù)庫,接下來我們就看看在cmd中如何使用數(shù)據(jù)庫吧.
首先我們在進入到mysql之后使用語句 查看所有的數(shù)據(jù)庫.
show databases
然后是 創(chuàng)建數(shù)據(jù)庫 persondb數(shù)據(jù)庫名
create database [if not exists] persondb
使用 切換數(shù)據(jù)庫
use persondb
展示所有的表格
show tables
創(chuàng)建表
create table user_table(id int,perid int,name varchar(20),joy varchar(50))engine='innoDB' default charset='utf8';
插入信息
insert into user_table(id,perid,name,joy) values(1,1,'nana','銷售員');
查找所有信息
select *from user_table;
新增字段
alter table user_table add(jiangj int,gongzi int);
更新信息
update user_table set jiangj = 1000,gongzi =6000 where id = 1;
刪除信息
delete from user_table where id = 1;
刪除表
drop table user_table;
刪除數(shù)據(jù)庫
drop database persondb;
修改字段的類型和名字
alter table [表名字] change [舊字段] [新字段] [新字段的類型] [新字段的約束]
刪除字段
alter table [表名字] drop 字段
找出獎金高于工資零點六的員工
select * from user_table where jiangj >gongzi * 0.6;
找出部門編號為1中所有經(jīng)理,部門編號為2中所有銷售員,還有既不是不銷售員又不是經(jīng)理但其工資大于或等于2000的所有員工的詳資料
select * from user_table where (perid = 1 and joy ='messness')or(perid = 2 and joy = 'xiaoshou') or (joy not in('messness','xiaoshou')and gongzi >= 2000);
查詢所有員工按工資排序
select *from user_table order by gongzi;
查詢每個部門的平均工資
select perid,avg(gongzi) from user_table group by perid;
查詢每種工作的,最高工資,最低工資, 人數(shù)
select joy,max(gongzi),min(gongzi),count(*) from user_table group by joy;
查詢出’文員’, ‘銷售員’工種下,所有人的工資總和
select joy,sum(gongzi) from user_table where joy in ('wenyuan','xiaoshou')group by joy;
查詢出每個工種下人數(shù)大于2的工種并顯示其人數(shù);
select joy,count()from user_table group by joy having count() >2;
排序
select score from scoreTable order by score // 升序
select * from scoreTable order by score desc // 降序
函數(shù)
count() 統(tǒng)計個數(shù)
max() 最大值
min() 最小值
avg() 平均值