常用查看指令
注意: [database_name] = 你要進入的數據庫名字 ex: use db; 表示進入數據庫db,後面若有類似情況,參看此例。
* 啓動mysql服務器: net start mysql
* 關閉mysql服務器: net stop mysql
* 進入數據庫: mysql -uroot -p
* 退出數據庫:exit or quit;
* 查看數據庫: show databases;
* 查看全部表: show tables;
* 切換數據庫: use [database_name];
* 查看錶結構: desc [table_name];
* 查看當前用戶: select user();
* 查看當前數據庫: select database();
* 查看當前數據庫版本: select version();
* 查看數據庫狀態: show status;
數據庫使用指令
* 創建數據庫: create database [database_name];
* 刪除數據庫: drop database [database_name];
* 創建表:
create table [table_name](id int auto_increment primary key,
name varchar(50),
sex varchar(20),
date varchar(50),
content varchar(100)
)default charset=utf8;
* 先判斷表是否存在,存在先刪除: drop table if exists student;
* 刪除表: drop table [table_name];
* 插入數據:insert into student values(null,'aa','male','1999-9-9','15465');
* 查詢表中的數據: select * from student;
* 修改某一條數據: update student set sex='男' where id=1;
* 刪除數據: delete from student where id=2;
* 修改表名: rename table [old_name] to [new_name]
其他指令
* and == 與
* or == 或
* between 在什麼之間 between age<5 and age >3;
* 排序 asc 升序 desc 降序 ex: select * from student order by id asc;
* in 查詢制定集合內的數據 ex: select * from student where id in (1,3,5);
* 分組查詢 and 聚合函數
select max(id),name,sex from student group by sex;
select min(date) from student;
select avg(id) as '求平均' from student;
select count(*) from student; #統計表中總數
select count(sex) from student; #統計表中性別總數 若有一條數據中sex爲空的話,就不予以統計~
select sum(id) from student;
* 查詢第i條以後到第j條的數據(不包括第i條)
select * from student limit 2,5; 顯示3-5條數據
聚類函數和其意義
* 集函數意義
* SUM ( ) 求和
* AVG ( ) 平均值
* COUNT ( ) 表達式中記錄的數目
* COUNT ( ) 計算記錄的數目
* MAX 最大值
* MIN 最小值
* VAR 方差
* STDEV 標準誤差
* FIRST 第一個值
* LAST 最後一個值
列操作
* MySql中增加一列
如果想在一個已經建好的表中添加一列,可以用:
alter table [table_name] add column [new_column_name] varchar(45) not null;
如果希望添加在指定的一列,可以用:
alter table [table_name] add column [new_column_name] varchar(45) not null after [cloumn_name];
如果想添加到第一列,可以用:
alter table [table_name] add column [new_column_name] varchar(45) not null first;
* MySql中刪除一列: alter table [table_name] drop column [column_name] ;
* MySql中修改一列:alter table [table_name] change [old_column_name] [new_column_name];
借鑑:
https://www.cnblogs.com/zhuyongzhe/p/7686105.html
https://www.jb51.net/article/136869.htm
https://www.cnblogs.com/zhangyuhang3/p/6873895.html
https://blog.csdn.net/qq_35409127/article/details/79760797