本篇文章記錄了Mysql數(shù)據(jù)庫的一些常規(guī)操作指令专筷,便于使用數(shù)據(jù)庫時查找。
col_name
列名稱 tbl_name
表名稱 operator
運算符 val
值 col
列 db_name
數(shù)據(jù)庫名稱 col_attr
數(shù)據(jù)類型 file_loc
文件位置
1.獲取數(shù)據(jù):
select col_name from tbl_name;
select * from tbl_name;//匹配所有列
select distinct col_name from tbl_name;//僅返回不同值
2.獲取滿足特定條件的數(shù)據(jù):
select col_name from tbl_name where col 運算符 值;
例子:
select * from orders where id = 3;
select * from orders where id = 3 or id = 4;
select * from orders where name like 'N%';//選取制定模式
select * from orders where 1 between 3;//選取兩值之間的數(shù)據(jù)
3.對數(shù)據(jù)進(jìn)行排序:
select col from tbl_name order by col asc;//生序排列
select col from tbl_name order by col desc;//降序排列
4.插入數(shù)據(jù):
insert into tbl_name values(val1,val2,...);
insert into tbl_name(col1,col2,...) values(val1,val2,...);//插入指定列
5.修改數(shù)據(jù):
update tbl_name set col_name = val1 where col = val2;
6.刪除數(shù)據(jù):
delete from tbl_name where col = val;
7.規(guī)定返回數(shù)據(jù)數(shù)目:
select col_name from tbl_name limit val;
```select top 50 percent * from tbl_name;//選取50%的記錄```
8.創(chuàng)建數(shù)據(jù):
```create database db_name;//創(chuàng)建數(shù)據(jù)庫```
```create table tbl_name(col1 attr1,col2 attr2,....);```
9.撤銷表:
```drop table tbl_name;```
```drop database db_name;```
```truncate table tbl_name;//僅刪除表格中數(shù)據(jù)
10.alter已有表添加融求、刪除、修改:
```alter table tbl_name add column col_name col_attr;```
```alter table tbl_name drop column col_name col_attr;```
```alter table tbl_name alter column col_name col_attr;```
11.備份復(fù)件:
```select col_name into tbl1 from tbl2;```
12.字段編碼格式改變:
例子:
```alter table tbl_name modify col_name col_attr character set gbk;//改為gbk編碼模式```
13.查看數(shù)據(jù):
```show full columns from tbl_name;//查看表單字符集設(shè)置```
```show tables;//查看所在數(shù)據(jù)庫所有表單```
```show databases;```
```describe tbl_name;//查看表單詳細(xì)信息```
14.導(dǎo)入文件:
```source file_loc;//導(dǎo)入外部sql文件```
例子:
```source D:\bookmark.sql;```
```load data local infile 'file_loc' into table tbl_name;//txt格式文件導(dǎo)入數(shù)據(jù)庫```
例子:
```load data local infile 'D:\excel.txt' into table tbl_name character set utf8 fields terminated by',';```