Mysql 學習筆記

 顯示所有數據庫 show databases;
 創建數據庫 create dababase stu;
 刪除數據庫drop database stu;
 切換到數據庫use stu;
 取得當前的數據庫,帶()表示函數:select database();

創建表(要切換到數據庫中)
mysql> create table stu_info(
    -> stu_id int(8),
    -> stu_name varchar(48),
    -> stu_sex char(2),
    -> stu_age int(8));
查看錶的結構
mysql> desc stu_info;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| stu_id   | int(8)      | YES  |     | NULL    |       |
| stu_name | varchar(48) | YES  |     | NULL    |       |
| stu_sex  | char(2)     | YES  |     | NULL    |       |
| stu_age  | int(8)      | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
刪除表
drop table stu_info;
查看數據庫中所有的表show tables;
主鍵 primary key
增加字段 alert table stu_info add column stu_card varchar(20);
 刪除字段alter table stu_info drop column stu_card ;
修改字段 alter table stu_info modify column stu_id int(20);

 

delete from stu_info where []
insert into stu_info() values ()
select * from stu_info where []

mysql 分業技術  選擇從第1條開始的3條數據
select * from stu_info limit 0,3 ;
選擇從第1條開始的3條數據
select * from stu_info limit 5,3 ;

直接從文件中讀取sql語句然後在mysql中執行

 /. d://che//mysql1//mysql.sql
例如
mysql.sql文件中有
use student;

select * from stu_info;

執行 /. d://che//mysql1//mysql.sql後結果爲

Database changed
+--------+----------+---------+---------+----------------+--------------+
| stu_id | stu_name | stu_sex | stu_age | stu_adr        | stu_birthday |
+--------+----------+---------+---------+----------------+--------------+
|      1 | 車燕兵   | 男      |      25 | cheyanbing.com | 2009-01-02   |
|      2 | che      | man     |      23 | gfdfgdsf       | 2008-12-23   |
|      3 | che      | man     |      23 | gfdfgdsf       | 2008-12-23   |
|      4 | che      | man     |      23 | gfdfgdsf       | 2008-12-23   |
|      5 | che      | man     |      23 | gfdfgdsf       | 2008-12-23   |
|      6 | che      | man     |      23 | gfdfgdsf       | 2008-12-23   |
|      7 | che      | man     |      23 | gfdfgdsf       | 2008-12-23   |
|      8 | che      | man     |      23 | gfdfgdsf       | 2008-12-23   |
|      9 | che      | man     |      23 | gfdfgdsf       | 2008-12-23   |
|     10 | che      | man     |      23 | gfdfgdsf       | 2008-12-23   |
|     11 | che      | man     |      23 | gfdfgdsf       | 2008-12-23   |
|     12 | cyb      | man     |      23 | jiangxiganzhou | 2009-02-21   |
+--------+----------+---------+---------+----------------+--------------+
12 rows in set (0.00 sec)

 

 

排序方式
asc  按升序   ascend升序
desc 按降序   descend降序
select * from stu_info order by stu_id asc, stu_age desc;//先按學號升序,再按年齡降序排

常用函數

計算記錄的行數 count()
 select count(*) from stu_info;//求所有記錄的條數,可以加條件表達式
        select count(stu_sex) from stu_info;//注意當stu_sex爲空的時候,不會被計算進去
 select count(*),count(stu_sex) from stu_info;
+----------+----------------+
| count(*) | count(stu_sex) |
+----------+----------------+
|        7 |              1 |
+----------+----------------+
對字段求和 sum()
 select sum(stu_age) from stu_info;//字段只能是數字型,不能爲其他類型
+--------------+
| sum(stu_age) |
+--------------+
|          175 |
+--------------+
求平均數函數 avg()
  select avg(stu_id) from stu_info;//空字段不參與求平均
+-------------+
| avg(stu_id) |
+-------------+
|      4.0000 |
+-------------+ 
日期函數
 select now();
+-------------+
| avg(stu_id) |
+-------------+
|      4.0000 |
+-------------+ 
將日期轉換爲天數 to_days()
  select to_days(stu_birthday) from stu_info;
+-----------------------+
| to_days(stu_birthday) |
+-----------------------+
|                733774 |
+-----------------------+
數學函數
 abs()
 mod()
 round()
字符串函數
 length(str)
 left(str,len)
 right(str,len)
 substring(str,pos,len)//下標從1開始
 ascii(str)
 concat(str1,str2,...)

 

創建外鍵關聯
 create table employee(
 id int primary key,  
 dep_id int,
 name varchar(20),
 constraint fk_emp_dept_id foreign key(dep_id) references department(id));
//constraint約束
刪除外鍵關聯關係
alter table employee drop foreign key  fk_emp_dept_id;
再加drop index fk_emp_dept_id on employee;
外鍵所在的表叫父表,該表叫子表,
以後操作的時候刪除父表id要同時刪除子表的記錄,否則麻煩就大了 。
修改外鍵
alter table employee add constraint fk_emp_dept_id foreign key(dep_id) references department(id);
顯示用於創建給定表的CREATE TABLE語句
show create table employee;
刪除主鍵
 alter table stu_info drop primary key ;

創建主鍵
alter table stu_info modify stu_id int primary key;
建立索引
 create index emp_index on employee(name);
刪除索引
drop index emp_index on employee;

多表聯合查詢
select employee.id,employee.dep_id,department.id,department.name from employee,department where employee.dep_id=department.id;

數據庫備份

//注意一定要在dos下操作,不能在mysql客戶端操作

備份一個數據庫
mysqldump -u root -p --opt student>d:/che/test.sql 
opt表示優化數據庫
備份所有數據庫
mysqldump -u root -p --all-database>d:/che/all.sql

備份數據庫中的單個表
mysqldump -u root -p --opt student stu_info>d:/che/stu_info.sql
備份數據庫中的多個表
mysqldump -u root -p --opt student stu_info employee>d:/double.sql

數據庫恢復


恢復一個數據庫
mysql -u root -p student <d:/che/test.sql
恢復所有數據庫
mysql -u root -p <d:/che/all.sql
恢復數據庫中的單個表
mysql -u root -p  student<d:/che/stu_info.sql

恢復數據庫中的單個表
mysql -u root -p  student<d:/che/double.sql


授予權限//如果沒有該用戶則創建該用戶
grant [權限] on [數據庫名].[表名] to ['用戶名']@['web服務器的ip地址'] identified by ['密碼']
with grant option;

所有權限
grant all privileges on student.* to [email protected]   identified by 'cyb';
賦予student數據庫所有權限
賦予部分權限
grant select,alter on student.* to [email protected]   identified by 'cyb';
回收權限
revoke select  on student.* from [email protected]

刪除用戶

delete from user where User='cyb'
記得使用commit;
 flush privileges;

修改密碼

 update user set password=PASSWORD('cheyanbing')where user='cyb1';
記得使用commit;
 flush privileges;

 

實現將查詢的結果記錄保存到文件中命令

select list from table into outfile '文件名';
例如:

select * from tbl_corporation into outfile 'd:/test.txt';

發佈了103 篇原創文章 · 獲贊 1 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章