mysql中的命令總結

2021-09-01 17:23:57 字數 3329 閱讀 6088

用了一段時間的mysql資料庫了,現在對mysql資料庫中經常用的命令小總結一下,以免以後忘記:

1,連線資料庫:

mysql -u root -p root
2,退出資料庫:

quit;或者exit;
3,顯示資料庫:

show databases;
4,在使用某個資料庫時,一定要先切換的該資料庫,命令為:

use databasename;
5,檢視某個資料庫裡面的有哪些表時:

show tables;
6,檢視某個表的結構:

desc tablename;
7,檢視當前時間和資料庫版本:

select now(),version();
8,建立刪除資料庫:

create database student;

drop database student;

9,建立刪除表:

create table falcatys(falcaty_id int not null auto_increment primary key,falcaty_name char(30) not null);

drop table falcatys;

10,顯示建立表的資訊:

show create table falcatys;
11,修改表中某一列的資訊:

alter table falcatys modify column falcaty_name char(30) character set utf8 not null;
12,  修改表的建立資訊:

alter table falcatys default charset=utf8;
13,插入刪除資料(一次可以插入一條或多條資料):

insert into falcatys(falcaty_name,falcaty_students_num) values('資源與環境學院',0);

delete * from falcatys where fallacy_id = 1;

14,查詢表中資料:

select * from falcatys;  --查詢表中的所有資料  

select fallacy_id,fallacy_name from falcatys; --查詢給定的列

15,新增列刪除列:

alter table professions add column falcaty_id int;

alter table professions drop column falcaty_id;

16,新增刪除主鍵約束:

alter table professions add constraint profession_id primary key professions(profession_id);

alter table professions drop primary key;

17,新增外來鍵約束:

alter table professions add constraint falcaty_profession_class foreign key(falcaty_id) references falcatys(falcaty_id);

alter table professions drop foreign key fallacy_profession_class;

18,新增刪除唯一約束:

alter table t_stu add constraint uk_stucode unique(stucode);

alter table t_stu drop index uk_stucode;

19, 重新命名表:

rename table t_stu to t_user;
20,匯入資料庫:

21,更新表中的資料:

update t_stu set stuname='jack'[where id=1] ;--修改id為1的名字為jack,  如果不寫中括號裡面的where條件,則會把所有的姓名都改為jack  

update t_stu set stuage=stuage+1; --將表中stuage這一列的數都加一

update t_stu set stuage=23 where id id(1,2,4,5); --將id號為1,2,4,5的 stuage改為23

update t_stu set stuage=27 where id between 1 and 4;--將id號在1和4之間的stuage改為27,相當於where id>=1 and id<=4;

update t_stu set stuage=24 where stuage is null;

update t_stu set stuage=21 where stuage is not null;

22,刪除表中所有資料:

truncate table t_stu;  --刪除t_stu表中的所有資料
23,去除重複的查詢:

select distinct vend_id from products;
24,分頁查詢:

select id,stuname,stuage,stuaddress from t_stu limit 0,5;--第一頁從0開始往後數5個顯示出來  

select id,stuname,stuage,stuaddress from t_stu limit 5,5;--第二頁從5開始往後數5個顯示出來

25,排序查詢:

select prod_id,prod_name,vend_id,prod_price from products order by prod_price asc,prod_name asc;--先按prod_price公升序排列,當prod_price相同時再按prod_name排序

MySQL命令總結

安裝 sudo apt get install mysql server 服務啟動服務 sudo service mysql start 停止服務 sudo service mysql stop 重啟服務 sudo service msyql restart 配置配置檔案目錄為 etc mysql ...

MySQL命令總結

mysql 這是乙個關係型資料庫,存在表的概念 結構 資料庫可以存放多少張表,每張表可以存放多少字段,每個字段可以存放多少記錄 dos命令運算元據庫 phpstudy使用終端開啟資料庫 其他選項 mysql工具 快捷建立資料庫 對資料庫進行增刪查改 分號,是資料庫的結束標誌 show databas...

mysql簡單命令總結

登入 mysql u root p 檢視所有的資料庫 show databases 建立乙個資料庫 create database 資料庫名 刪除乙個資料庫 drop database 資料庫名 選擇要操作的sql資料庫 use 資料庫名 檢索單個列 輸入select pro name from p...