mysql個人使用 mysql整理(個人)

2021-10-17 23:40:16 字數 3337 閱讀 1420

注意:以下命令都是在linux系統下執行的:

1、驗證mysql是否安裝成功:

mysqladmin --version

2、連線mysql伺服器:

mysql -u root -p 之後輸入密碼

3、退出:

exit

4、建立資料庫:

create database zjx1;

5、刪除資料庫:

drop database zjx1;

6、建立資料表:

1 create table zjx1(2 ->runoob_id int not null auto_increment,3 -> name varchar(100) not null,4 -> age varchar(40) not null,5 ->primary key ( runoob_id )6 -> )engine=innodb default charset=utf8;

7、刪除資料表:

drop table zjx1;

8、在資料表中插入資料:

insert into zjx1->(name,age)->values-> ("sunny",18);

9、檢視資料表中所有資料:

select * from zjx1;

10、一次插入多條資料:

insert into zjx1->(name,age)->values-> ("1",11),("2",22),("3",33);

11、檢視資料表中資料:

select column_name,column_name

from table_name

[where clause]

[limit n][ offset m]

#查詢語句中你可以使用乙個或者多個表,表之間使用逗號(,)分割,並使用where語句來設定查詢條件。

#select 命令可以讀取一條或者多條記錄。

#你可以使用星號(*)來代替其他字段,select語句會返回表的所有字段資料

#你可以使用 where 語句來包含任何條件。

#你可以使用 limit 屬性來設定返回的記錄數。

#你可以通過offset指定select語句開始查詢的資料偏移量。預設情況下偏移量為0。

12、更新**中的字段:

update zjx1 set name="xiaohua" where id=3;

13、刪除**中的資料:

delete from zjx1 where id=5;

14、like與where連用:like 匹配/模糊匹配,會與 % 和 _ 結合使用。

'%a'    //以a結尾的資料

'a%'    //以a開頭的資料

'%a%'    //含有a的資料

'_a_'    //三位且中間字母是a的

'_a'    //兩位且結尾字母是a的

'a_'    //兩位且開頭字母是a的

15、union:連線兩個以上的 select 語句的結果組合到乙個結果集合中

union 語句:用於將不同表中相同列中查詢的資料展示出來;(不包括重複資料)

union all 語句:用於將不同表中相同列中查詢的資料展示出來;(包括重複資料)

使用形式如下:

select 列名稱 from 表名稱 union select 列名稱 from 表名稱 order by 列名稱;

select 列名稱 from 表名稱 union all select 列名稱 from 表名稱 order by 列名稱;

16、排序

select * from zjx1 order by age asc;     //公升序

select * from zjx1 order by age desc;    //降序

17、去重:select name,count(*) from t group by name;

18、統計:select name,sum(singin) as aaa from t group by name with rollup;

19、增刪改查 資料表名稱、字段、預設值等

刪除i欄位:

# aa為資料表

alter table aa drop i;

新增i欄位:

alter table aa add i int;

新增j欄位到第一列:

alter table aa add j int first;

新增j欄位到指定列後面:

alter table aa add j int after c;

修改指定欄位的型別:

alter table aa modify c char(10);

修改指定欄位的名稱:

alter table aa change c g int;

alter對null和預設值的影響:

alter table aa modify j int not null default 10000;

修改欄位的預設值:

alter table aa alter i set default 1000;

刪除字段預設值:

alter tableaa alter i drop default;

修改表名:

alter table aa rename to aaa;

20、顯示表字段結構詳細資訊

show columns from a;

21、複製表

# 只複製表結構到新錶

create table 新錶 select* from 舊表 where 1=2;

或create table 新錶 like 舊表 ;

# 注意上面兩種方式,前一種方式是不會複製時的主鍵型別和自增方式是不會複製過去的,而後一種方式是把舊表的所有字段型別都複製到新錶。

# 複製表結構及資料到新錶1 create table 新錶 select *from 舊表

# 複製舊表的資料到新錶(假設兩個表結構一樣)1 insert into 新錶 select *from 舊表

# 複製舊表的資料到新錶(假設兩個表結構不一樣)1 insert into 新錶(欄位1,欄位2,.......) select 欄位1,欄位2,...... from 舊表

22、select version( ) //伺服器版本資訊

select database( ) //當前資料庫名 (或者返回空)

select user( ) //當前使用者名稱

show status //伺服器狀態

show variables //伺服器配置變數

23、匯出資料庫中的資料

mysql> select *from runoob_tbl-> into outfile '/tmp/runoob.txt';

mysql 字串中取整 MySQL取整

對乙個表取任意隨機數 select from tmp xf test where id select floor rand select max id from tmp xf test order by id limit 1 有條件性的取隨機數 select from tmp xf test whe...

mysql 日期整數 mysql中計算日期整數差

timestampdiff 語法 timestampdiff interval,datetime expr1,datetime expr2 返回日期或日期時間表示式datetime expr1 和datetime expr2the 之間的整數差 interval 間隔 frac second。表示間...

mysql 日期整數 mysql中計算日期整數差

timestampdiff 語法 timestampdiff interval,datetime expr1,datetime expr2 返回日期或日期時間表示式datetime expr1 和datetime expr2the 之間的整數差 interval 間隔 frac second。表示間...