1. 程式人生 > >MySQL資料庫簡單操作及事務和索引

MySQL資料庫簡單操作及事務和索引

分享MySQL資料庫簡單操作及事務和索引的筆記,希望對大家有所幫助。

MySQL資料庫操作:
[[email protected] ~]# mysql -u root -p //進入資料庫中
MySQL資料庫簡單操作及事務和索引

mysql> show databases; //檢視所有資料庫

MySQL資料庫簡單操作及事務和索引
mysql> use mysql; //進入mysql資料庫

MySQL資料庫簡單操作及事務和索引
mysql> show tables; //檢視資料庫中的表

MySQL資料庫簡單操作及事務和索引

mysql> desc user; //查看錶的結構
MySQL資料庫簡單操作及事務和索引

mysql> create database auth; //建立新的空庫
MySQL資料庫簡單操作及事務和索引

mysql> show databases;
如圖,顯示所有庫命令下,剛建立的庫在其中
MySQL資料庫簡單操作及事務和索引


[[email protected] ~]# ls /usr/local/mysql/data/
MySQL資料庫簡單操作及事務和索引
如圖多了一個auth目錄
mysql> show tables; //檢視auth資料庫中的表
MySQL資料庫簡單操作及事務和索引
庫裡沒有表

mysql> create table users(user_name CHAR(16) NOT NULL,user_passwd CHAR(48)DEFAULT'',PRIMARY KEY (user_name));
//在auth庫中建立users表,並指定主鍵

MySQL資料庫簡單操作及事務和索引
mysql> desc users; //檢視users表的結構

MySQL資料庫簡單操作及事務和索引
mysql> drop table auth.users; //刪除auth庫中的users表
MySQL資料庫簡單操作及事務和索引

mysql> insert into users(user_name,user_passwd) values('zhangsan',password('123123'));
//在users表中插入資料內容zhangsan,密碼為密文密碼
MySQL資料庫簡單操作及事務和索引

mysql> insert into users(user_name,user_passwd) values('lisi','123123');
//在users表中插入資料內容lisi,密碼為明文密碼123123
MySQL資料庫簡單操作及事務和索引
mysql> insert into users values('wangwu','123abc');
//在users表中插入資料內容wangwu,密碼為明文密碼123abc
注:在插入新的資料內容時,如果這條記錄完整包括表中所有欄位的值,則插入語句中指定欄位的部分可省略
MySQL資料庫簡單操作及事務和索引

mysql> insert into users values('jack',password('123abc'));
//在users表中插入資料內容wangwu,密碼為密文密碼
MySQL資料庫簡單操作及事務和索引

mysql> select from users; //檢視users表中資料記錄
MySQL資料庫簡單操作及事務和索引
mysql> select
from users \G // \G 表示豎狀顯示出來
MySQL資料庫簡單操作及事務和索引

mysql> update users SET user_passwd=PASSWORD('') WHERE user_name='lisi';
//修改users表中使用者名稱為lisi的記錄,將密碼子串設為空值
mysql> select * from users;//檢視修改後的結果
MySQL資料庫簡單操作及事務和索引

跳過授權表重置root密碼----破解root密碼
[[email protected] ~]# vi /etc/my.cnf //修改mysql主配置檔案

skip_grant_tables

MySQL資料庫簡單操作及事務和索引
[[email protected] ~]# service mysqld restart
[[email protected] ~]# mysql //直接輸 mysql 回車
MySQL資料庫簡單操作及事務和索引

mysql> update mysql.user set authentication_string=password('123456') where user='root';
//在mysql庫users表中將使用者名稱為root的密碼修改為123456的密文形式
MySQL資料庫簡單操作及事務和索引
mysql> flush privileges; //重新整理許可權
MySQL資料庫簡單操作及事務和索引
修改完成後進入的mysql主配置檔案中將skip_grant_tables刪除後重啟mysql
[[email protected] ~]# service mysqld restart
MySQL資料庫簡單操作及事務和索引
現在用mysql登入會報錯
[[email protected] ~]# mysql -u root -p
MySQL資料庫簡單操作及事務和索引

mysql> delete from users where user_name='lisi'; //刪除auth庫中users表中的使用者名稱為lisi的記錄
MySQL資料庫簡單操作及事務和索引
mysql> drop database aa; //刪除資料庫aa
MySQL資料庫簡單操作及事務和索引

索引
mysql> use imployee_salary;
mysql> show tables; //檢視imployee_salary庫中的表
MySQL資料庫簡單操作及事務和索引
mysql> select * from IT_salary; //查詢IT_salary表中的資料記錄
MySQL資料庫簡單操作及事務和索引
普通索引
mysql> create index salary_index on IT_salary(薪資);
//為IT_salary表的薪資列,建立普通索引salary_index
MySQL資料庫簡單操作及事務和索引

mysql> show index from IT_salary; //檢視普通索引
MySQL資料庫簡單操作及事務和索引

唯一性索引
mysql> create unique index salary_unique on IT_salary(姓名);
//IT_salary表的姓名列建立唯一性索引salary_unique
MySQL資料庫簡單操作及事務和索引
mysql> show index from IT_salary;
MySQL資料庫簡單操作及事務和索引
mysql> alter table IT_salary add primary key(員工ID);
MySQL資料庫簡單操作及事務和索引
這個報錯,是提醒已有主鍵了,之前已經設定過主鍵了

事務,將操作語句做為一個整體提交後進行批量執行--
mysql> use auth;
mysql> begin;
mysql> insert into users values('tom',password('123abc'));
mysql> insert into users values('lucy',password('123abc'));
MySQL資料庫簡單操作及事務和索引
然後再開一個遠端登入看一下,有木有將資料記錄插入進去表中
MySQL資料庫簡單操作及事務和索引
mysql> commit; //提交
MySQL資料庫簡單操作及事務和索引
再看一下
MySQL資料庫簡單操作及事務和索引
現在有了,事務一般用於指令碼中。