MySQL資料庫的基礎操作

2021-07-09 03:58:18 字數 1549 閱讀 2400

1.插入資料: 命令:insert into 《表名》 【(欄位名1).....】values (值n)

insert into test (id, name) values(1,'xiaozhang'); 

插入id為1,name為xiaozhang的表。

insert into test(id,name)  values (2,『xiaoyu』),(3,xiaoli);

批量插入方法,提公升效率。

2.查詢資料:命令: select 《欄位1,欄位2,....> from 《表名》 where《表示式》

select * from test;

檢視表test中的所有資料,一般不推薦這樣查詢,拖慢效率。

select  *  from  test limit 2;

查詢最前面的兩行資料。

select * from test order by id desc limit 0,2;

select * from test order by id asc limit 0,2;

以id查詢表的第0到2行,desc降序,asc公升序。

select * from test order where id =2;

select * from test order where name =' xiaozhang';

3.修改表中的資料: 命令:update 表名 set 字段=新值,....  where 條件

update test set name='xiaoyu' where id =1;

把id為1的值的name中的內容修改為xiaoyu。

update test set name='xiaoyu' where name='xiaoli' ;

update test set name='xiaoyu' ;

把name這項的內容全部改為xiaoyu.

4.刪除表中的資料:命令:delete from 表名 where 表示式

delete from test where id=1;

刪除表test中編號為1的記錄。

delete * from test;

刪除表中的所有資料。

5.在表中增刪改欄位: 命令:alter  table 表名  add  字段  型別  其他;

alter table test add *** char (4);

增加性別列***。

alter table test add age int(4) after name;

在name列後增加age列。

6.清空表中的內容:命令:truncate table 表名;

truncate table test;

清空表中的所有內容。

7.更改表名:命令:rename table 原表名 to 新錶名;

rename table test to mysyu;

更改表名為mysyu。

8.刪除表:命令:drop table 《表名》

drop table test;

刪除表名為test的表。                         

MySQL基礎 MySQL 的資料庫操作!

前面章節陸續完成了 mysql 的介紹和安裝配置,至此已經可以進行資料庫操作語言的學習了。作為學習筆記,本章更多地採用列表地形式簡潔地記錄操作命令,便於後續查閱和使用,而且資料庫操作語言的介紹將結合 python 一起。點此處加人我們的學習基地!根據第四章內容,首先啟動 mysql 伺服器,並登入資...

MySQL 資料庫基礎操作

1.建立資料庫 建立乙個名為db1的資料庫 create database db1 tips 當我們建立資料庫沒有指定字符集和校驗規則時,系統使用預設字符集 utf8 檢視系統支援的字符集 show charset 建立乙個使用utf8字符集的資料庫 create database test1 ch...

MySQL資料庫基礎操作

net start mysql net stop mysql mysql u 登入賬戶名 預設情況下是root p 登入密碼 或者 mysql u 登入賬戶名 預設情況下是root p 回車 在系統提示的password後面輸入你的 登入密碼 mysql h ip u 登入賬戶名 預設情況下是roo...