01 MySQL基本操作

2023-01-29 12:51:20 字數 2675 閱讀 9362

一、概述

1、什麼是資料庫 ?

答:資料的倉庫,如:在atm的示例中我們建立了一個 db 目錄,稱其為資料庫

2、什麼是 mysql、oracle、sqlite、access、ms sql server等 ?

答:他們均是一個軟體,都有兩個主要的功能:

a. 將資料儲存到檔案或記憶體

b. 接收特定的命令,然後對檔案進行相應的操作

3、什麼是sql ?

答:上述提到mysql等軟體可以接受命令,並做出相應的操作,由於命令中可以包含刪除檔案、獲取檔案內容等眾多操作,對於編寫的命令就是sql語句。sql是一種專門用來與資料庫通訊的語言。

輸入回車,見下圖表示安裝成功:

三、資料庫操作

1、顯示資料庫

show databases;

預設資料庫:

mysql - 使用者許可權相關資料

test - 用於使用者測試資料

information_schema - mysql本身架構相關資料

2、建立資料庫

# utf-8

create database 資料庫名稱 default charset utf8 collate utf8_general_ci;

# gbk

create database 資料庫名稱 default character set gbk collate gbk_chinese_ci;

3、使用資料庫

use db_name;

顯示當前使用的資料庫中所有表:show tables;

4、使用者管理

建立使用者

create user '使用者名稱'@'ip地址' identified by '密碼';

刪除使用者

drop user '使用者名稱'@'ip地址';

修改使用者

rename user '使用者名稱'@'ip地址'; to '新使用者名稱'@'ip地址';;

修改密碼

set password for '使用者名稱'@'ip地址' = password('新密碼')

5、授權管理

show grants for '使用者'@'ip地址' -- 檢視許可權

grant 許可權 on 資料庫.表 to '使用者'@'ip地址' -- 授權

revoke 許可權 on 資料庫.表 from '使用者'@'ip地址' -- 取消許可權

四、資料表基本

1、建立表

create table 表名(

列名 型別 是否可以為空,

列名 型別 是否可以為空

)engine=innodb default charset=utf8

是否可以為空

預設值自增

主鍵外來鍵

2、刪除表

drop table 表名

3、清空表

delete from 表名

truncate table 表名

4、修改表

新增列:alter table 表名 add 列名 型別

刪除列:alter table 表名 drop column 列名

修改列:

alter table 表名 modify column 列名 型別; -- 型別

alter table 表名 change 原列名 新列名 型別; -- 列名,型別

新增主鍵:

alter table 表名 add primary key(列名);

刪除主鍵:

alter table 表名 drop primary key;

alter table 表名 modify 列名 int, drop primary key;

新增外來鍵:alter table 從表 add constraint 外來鍵名稱(形如:fk_從表_主表) foreign key 從表(外來鍵欄位) references 主表(主鍵欄位);

刪除外來鍵:alter table 表名 drop foreign key 外來鍵名稱

修改預設值:alter table testalter_tbl alter i set default 1000;

刪除預設值:alter table testalter_tbl alter i drop default;

5、基本資料型別

mysql的資料型別大致分為:數值、時間和字串

五、表內容操作

1、增insert into 表 (列名,列名...) values (值,值,值...)

insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...)

insert into 表 (列名,列名...) select (列名,列名...) from 表

2、刪delete from 表

delete from 表 where id=1 and name='alex'

3、改update 表 set name = 'alex' where id>1

4、查ect * from 表

select * from 表 where id > 1

select nid,name,gender as gg from 表 where id > 1

mysql 基本操作

一 連線mysql。格式 mysql h主機地址 u使用者名稱 p使用者密碼 1 連線到本機上的mysql。首先開啟dos視窗,然後進入目錄mysql bin,再鍵入命令mysql u root p,回車後提示你輸密碼.注意使用者名稱前可以有空格也可以沒有空格,但是密碼前必須沒有空格,否則讓你重新輸...

mysql基本操作

第一招 mysql服務的啟動和停止 net stop mysql net start mysql 第二招 登陸mysql 語法如下 mysql u使用者名稱 p使用者密碼 鍵入命令mysql uroot p,回車後提示你輸入密碼,輸入12345,然後回車即可進入到mysql中了,mysql的提示符是...

mysql 基本操作

修復表 當mysql服務在執行時,也可以用mysql內建命令mysqlcheck來修復。語法 mysqlcheck r 資料庫名 表名 uuser ppass mysqlcheck r sports results mytable uuser ppass sports results.mytable...