mysql4語法 mysql四 資料操作

2021-10-22 05:48:22 字數 2768 閱讀 1860

一、介紹

mysql資料操作: dml 資料庫操縱語言

在mysql管理軟體中,可以通過sql語句中的dml語言來實現資料的操作,包括

使用insert實現資料的插入

update實現資料的更新

使用delete實現資料的刪除

使用select查詢資料以及

主要內容包括:

插入資料

更新資料

刪除資料

查詢資料

二、插入資料insert

1.插入完整資料(順序插入)

語法一:

insert into 表名(欄位1,欄位2,欄位3…欄位n) values(值1,值2,值3…值n);

語法二:

insert into 表名 values (值1,值2,值3…值n);

2.指定字段插入資料

語法:insert into 表名(欄位1,欄位2,欄位3…) values (值1,值2,值3…);

3.插入多條記錄

語法:insert into 表名 values

(值1,值2,值3…值n),

(值1,值2,值3…值n),

(值1,值2,值3…值n);

4.插入查詢結果

語法:insert into 表名(欄位1,欄位2,欄位3…欄位n) select (欄位1,欄位2,欄位3…欄位n) from 表2 where …;

三、更新資料update

語法:update 表名 set

欄位1=值1,

欄位2=值2,

where condition;

示例:update mysql.user set password=password(『123』) where user=』root』 and host=』localhost』;

四、刪除資料delete

語法:delete from 表名 where conition;

示例:delete from mysql.user where password=』』;

練習:更新mysql root使用者密碼為mysql123

update mysql.user set password=password(『mysql123』) where user=』root』;

刪除除從本地登入的root使用者以外的所有使用者

drop mysql.user user@'localhost'

delete from mysql.user where user=』root』 and host='localhost';

五、查詢資料select

單錶查詢:

多表查詢:

六、許可權管理

1、授權表

user #該錶放行的許可權,針對:所有資料,所有庫下所有表,以及表下的所有字段

db #該錶放行的許可權,針對:某一資料庫,該資料庫下的所有表,以及表下的所有字段

tables_priv #該錶放行的許可權。針對:某一張表,以及該錶下的所有字段

columns_priv #該錶放行的許可權,針對:某乙個字段

2、按**釋:

user:放行db1,db2及其包含的所有

db:放行db1,及其db1包含的所有

tables_priv:放行db1.table1,及其該錶包含的所有

columns_prive:放行db1.table1.column1,只放行該欄位

3、許可權相關的操作

建立使用者

create user 'test'@'1.1.1.1' identified by 'test';

create user 'test'@'192.168.1.%' identified by 'test';

create user 'test'@'%' identified by 'test';

授權:對資料夾,對檔案,對檔案某一字段的許可權

檢視幫助:help grant

常用許可權有:select,update,alter,delete

all可以代表除了grant之外的所有許可權

針對所有庫的授權:*.*

grant select on *.* to 'test1'@'localhost' identified by 'test2'; #只在user表中可以查到test使用者的select許可權被設定為y

針對某一資料庫:db1.*

grant select on db1.* to 'test2'@'%' identified by 'test2'; #只在db表中可以查到test2使用者的select許可權被設定為y

針對某乙個表:db1.t1

grant select on db1.t1 to 'test3'@'%' identified by 'test3';  #只在tables_priv表中可以查到test3使用者的select許可權

針對某乙個字段:

grant select (id,name),update (age) on db1.t3 to 'test4'@'localhost' identified by 'test4'; #可以在tables_priv和columns_priv中看到相應的許可權

select * from tables_priv where user='test4'\g

select * from columns_priv where user='test4'\g

刪除許可權

revoke select on db1.* from 'test'@'%';

MYSQL 4 檢索資料

select distinct vend id from products 不能部分使用distinct,distinct關鍵字應用於所有列而不是前置它的列。如果給出select distinct vend id,prod price,除非指定的兩個列都不同,否則所有行都被檢索出來。select p...

mysql四種啟動方式 mysql 4種啟動方式

mysql 4種啟動方式 都是去呼叫mysqld檔案 1.mysqld 啟動 進入mysqld檔案所在目錄 libexec mysqld mysqld defaults file my.cnf user mysql 2.mysqld safe 啟動 進入mysqld safe所在目錄 bin mys...

mysql4種事務 Mysql事務四種隔離級別

首先可查詢mysql自動提交狀態 select autocommit 結果如下 autocommit 1 可通過命令將資料庫事務修改為不自動提交 set autocommit 0 mysql四種事務隔離級別 1.讀未提交 read uncommitted 也叫髒讀 助記骯髒 設定事務隔離級別為 讀未...