linux中 Mysql使用語法

2021-09-21 02:23:01 字數 4174 閱讀 6413

1.mysql服務啟動

service mysqld start

mysql初始化環境配置

沒有設定密碼 ,執行mysql -u root 登入伺服器

密碼配置

檢視目前使用者

• select user,host,password from mysql.user

• 修改root密碼:set password for root@localhost=密碼;set password for [email protected]=密碼

刪除匿名使用者

• 檢視是否有匿名使用者:select user,host from mysql.user;

• 刪除匿名使用者:deleter from mysql.user where user=』 ';

• 再次檢視:select user,host from mysql.user;

• 重新整理:flush privileges

插入mysql新使用者

insert into mysql.user(host,user,password)values(「localhost」,「yourusername」,password(「密碼」));

建立新的database

create database 『資料庫名字』 default character set utf8 collate utf8_general_ci;

本地使用者賦予所有許可權

grant all privileges on 資料庫.* to 使用者名稱@localhost identified by 『密碼』

給賬號開通外網許可權

grant all privileges on 資料庫.* to 名字@『%』 identified by 『密碼』

mysql開啟關閉及查詢時間版本指令:

•開啟mysql服務 net start mysql 或 sudo service mysql start

•重啟服務 sudo service mysql restart

•檢視看程序中是否存在mysql服務 ps ajx|grep mysql

•進入mysql資料庫控制台命令: mysql -u使用者名稱 -p密碼

•檢視版本命令: select version();

•顯示當前時間命令: select now();

•退出mysql命令: quit或exit

•結束mysql服務 net stop mysql 或 sudo service mysql stop

修改使用者名稱及密碼相關指令:

•1、修改密碼格式命令: set password for 使用者名稱@localhost = password(『新密碼』);

•2、修改密碼格式命令: mysqladmin -u使用者名稱 -p舊密碼 password 新密碼;

•修改使用者名稱命令:

進入mysql庫: use mysql;

然後輸入: update user set user =『新使用者名稱』 where user =『舊使用者名稱』;

建立庫的相關指令:

•建立資料庫命令: create database 資料庫名 charset=utf8;

•顯示所有的資料庫: show databases;

•刪除資料庫命令: drop database 資料庫名;

•匯入(連線)資料庫命令: use 資料庫名

•檢視當前使用的資料庫命令: select database();

•當前資料庫包含的表資訊命令: show tables;

show create table 表名; 展示詳細屬性

•建立表命令: create table 表名 ((欄位名 型別 屬性),(欄位名 型別 屬性));

•修改表名: alter table 表名 rename to 新錶名;

•獲取表結構命令: desc 表名; 或者 show columns from 表名;

•刪除表命令: drop table 表名;

•修改字段命令: alter table 表名 change 原欄位名 新欄位名 資料型別;

•修改資料型別命令: alter table 表名 modify 欄位名 新資料型別;

•刪除乙個字段命令: alter table 表名 drop 欄位名;

•將字段1放在欄位2後面命令: alter table 表名 modify 欄位名1 資料型別 after 欄位名2;

插入資料相關命令:

•插入資料命令: insert into 表名 values (新增的資料,) #按列表豎列型別新增以,分隔

insert into 表名 (字段,字段) values (新增的資料,)

•檢視表中所有資料命令: select * from 表名;

•檢視相關型別資料命令: select 字段,字段 from 表名;

•查詢指定字段命令: select 字段 from 表名;

•查詢前幾行資料命令: 如:檢視表中前行資料 select * from 表名 limit 0,2;

•插入資料型別命令: alter table 表名 add 字段 資料型別;

•清空表內資料命令: delete from 表名;

•清空表 自增id,下次從1開始 truncate table 表名;

•第二種

(注意:這個是你通過delete from table 之後 設定的。不然不起作用)

如果表中的資料還有用,那麼需要從特定的某乙個值開始自動增長的話,做法如下

比如你想讓id從2開始自動增長,sql如下

alter table jx_pcmx auto_increment 2;

alter table jx_pcmx auto_increment 此處寫你想讓id從幾開始增長的數字;

•刪除表中資料命令: 如:刪除表中編號為1的記錄 delete from 表名 where id=1;

•修改表中資料命令: update 表名 set 字段=新值 where條件 如:update 表名 set name=』mary』where id=1;

•在表中增加字段命令: alter table 表名 add欄位 型別 其他;

—例如:在表myclass中新增了乙個欄位passtest,型別為int(4),預設值為空

mysql> alter table myclass add passtest int(4) default 『』;

•選取字段插入多條記錄命令: insert into 表名(欄位名1,欄位名2,…) values(值1,值2,…),(值1,值2,…)…;

•全部字段插入多條記錄命令: insert into 表名 values(值1,值2,…),(值1,值2,…)…;

•使用as 給字段起別名命令: select 原欄位名 as 新欄位名 from 表名; 查詢出來的結果欄位名不是原欄位名,變成了新欄位名

•使用distinct可以消除重複資料 select distinct 欄位名,distince 欄位名 from student; 查詢出來的性別(欄位名)和年齡(欄位名)沒有重複的資料

條件查詢:

使用where子句對錶中的資料篩選,結果為true的行會出現在結果集中

where後面支援多種運算子,進行條件的處理

•比較運算子: > < <= >= = != is

select * from 表名 where 條件;

•邏輯運算子:and or not

select * from 表名 where 條件;

•範圍查詢 between…and…

例: (左閉右閉,左右都包含)

select 字段 from 表名 where 字段 between 開始區間 and 結束區間;

•模糊查詢 like %表示任意多個任意字元 表示乙個任意字元

例: % 和 _ 可以加在任意位置、

select * from 表名 where 字段 like 『字元元素%』;

select * from 表名 where 字段 like 『%字元元素』;

select * from 表名 where 字段 like '字元元素』;

select * from 表名 where 字段 like 『_字元元素』;

•判非空 is not null

例: select * from 表名 where 字段 is not null;

MySQL中索引的使用語法

create table index student1 sid int unsigned not null auto increment,xing char 10 not null default ming char 10 not null default sno char 10 not null ...

mysql常用語法 MySQL常用語法

更新資料 update 表名 set 健名 更改的值 where 建 刪除資料 delete from 表名 where 索引 create index 索引名稱 on 表名 鍵名 建立試圖 create view 檢視名 鍵,鍵 as select matchno,abs won lost fro...

mysql基本常用語法 mysql 常用語法

1.1.開啟 service mysql start 1.2.關閉 service mysql stop 1.3.重啟 service mysql restart 2.1.密碼明文 語法 mysql u使用者名稱 p使用者密碼 舉例 mysql uroot p123456 2.2.密碼密文 語法 m...