常用的操作
- 命令行直接進入mysql(這里的前提示先進行mysql環(huán)境變量配置,管理員進入)
mysql -u root -p
Enter password: 123456 #輸入自己的密碼
- 查看當前服務(wù)器存在的全部數(shù)據(jù)庫
show databases;
- 創(chuàng)建數(shù)據(jù)庫
create database test; --test 表示數(shù)據(jù)庫名稱
- 使用當前哪個數(shù)據(jù)庫,如test數(shù)據(jù)
use test;
- 查看當前數(shù)據(jù)庫存在的表,如test數(shù)據(jù)庫中的全部表
show tables;
- 判斷數(shù)據(jù)庫是否存在
drop database if exists db_name;
- 判斷表是否存在
drop table if exists table_name;
- 創(chuàng)建表
create table tabletest(name vachar(20),age int(10))
- 導入數(shù)據(jù)庫如.sql文件(即 E:/mytest.sql)
use database;
source E:/mytest.sql;
- 刪除表
drop table tabletest;
- 清空表
delete table tabletest;
設(shè)置密碼,這里必須要進行管理員的cmd
use mysql;
delete from User where User="";
update User set Password=PASSWORD('你的密碼') where User ='root'
常見查詢案例
- 查詢test表中的全部數(shù)據(jù)
語法:select*from 表名 [where 條件];
select * from `test`;
- 查詢test表中username為小明的數(shù)據(jù)
select * from `test` where `username`='小明';
- 查詢表中某些字段的全部數(shù)據(jù)
語法:select field1,field2,...fieldn... from 表名 [where 條件];
select id,username,password from `test`; //查詢表的test全部數(shù)據(jù),但是只顯示id,username,password三個字段
- 查詢不重復數(shù)據(jù)
distinct添加這個關(guān)鍵字,其他和查詢語句一樣
語法:select distinct 字段 from 表名;
select distinct `username` from `test`; --用戶名不相同的數(shù)據(jù)
- 條件查詢
select 字段 from 表名 where 條件;
select * from test where username='小明'; -- 查詢username為小明的語句
- 排序查詢
select * from 表名 [where 條件] [ order by field1 [desc/asc],field2 [desc/asc]... ]; desc是降序,asc是升序,order by 默認是升序
select * from test where `age` order by desc;--年齡降序排序
- 限制符查找
select ... [limit 起始偏移量,行數(shù)]; - 查詢中間數(shù)據(jù)如查找5-9中的這些數(shù)據(jù)
select * from test where age order by desc limit 5,9