mysql資料庫基本操作學習筆記(1/2)

來源:互聯網
上載者:User

以下以資料庫”ceshi”為例

1、串連資料庫

 代碼如下 複製代碼

mysql -u username -p password

2、建立/刪除資料庫

 代碼如下 複製代碼

建立:create database ceshi;

刪除:drop database ceshi;

3、建立/刪除資料表

建立:

 代碼如下 複製代碼
create table students (sid int(10) auto_increment primary key,name varchar(255),course varchar(255),score int(10)) ;

刪除:

 代碼如下 複製代碼
drop table students;

設定資料表編碼:

 代碼如下 複製代碼
alter table `students` default character SET utf8 collate utf8_general_ci;

4、插入資料

 代碼如下 複製代碼

•單條插入 :insert into students (name,course,score) values(value1,value2,value3);
•多條插入:insert into students (name,course,score) select value1[0],value1[1],value1[2] union select value2[0] ,value2[1],value2[2] union……   www.111cn.net

•從另外的一張表中讀取多條資料添加到新表中:

 代碼如下 複製代碼
insert into students(col1,col2,col3) select a,b,c from tableA ;

•從其他的多張表中讀取資料添加到新表中:

 代碼如下 複製代碼
insert ioto tableName(col1,col2,col3)  select a,b,c from tableA where a=1  union all select a,b,c from tableB where a=2   

        

上邊代碼中的union all如果換成union,則相同記錄只插入一次,不會重複插入。

•上邊代碼中的into都可以省略!

5、order by語句

 代碼如下 複製代碼

select * from students order by score (asc);    從低往高排,預設,asc可省去

select * from students order by score desc;      從高往低排

6、group by語句

 代碼如下 複製代碼

select * from students group by course;       查詢資料按課程分組,只顯示查詢到的第一條

select * from students group by course order by socre;         order by

必須在 group by之後,group by 比order by先執行,order by不會對group by 內部進行排序,如果group by後只有一條記錄,那麼order by 將無效。要查出group by中最大的或最小的某一欄位使用 max或min函數。

--查看學生表的全部資料

 代碼如下 複製代碼

  select * from studio

  --插入一個新的學生資訊

 代碼如下 複製代碼

  insert into studio(st_name,st_sex,st_age,st_add,st_tel) values("黃蘭淇",0,36,'南充','13943943334')

  --查看class全部資料

 代碼如下 複製代碼

  select * from class

  --向class表增加兩條條資料

 代碼如下 複製代碼

  insert into class(cl_class,cl_coding,cl_o_time,cl_remark) values('新電實訓班','GXA-ncs-001','2008-03-11','都是很優秀的朋友')

  insert into class(cl_class,cl_coding,cl_o_time) values('阿壩師專實訓班','GXA-ABSZ-001','2008-03-11')

  --更新一條的資料 條件的重要性

 代碼如下 複製代碼

  update class set cl_remark='真的是不錯' where cl_id=5

  --刪除一條資料 條件的重要性

 代碼如下 複製代碼

  delete from class where cl_id=7

  --修改欄位標題

 代碼如下 複製代碼

  select cl_id as '班級主鍵',cl_class as '班級名稱' from class

  select 名字=st_name from studio

  --使用文字串

 

 代碼如下 複製代碼
 select '名字是:',st_name from studio

  --=============條件稍微複雜點的查增刪改==============

 

 代碼如下 複製代碼

 --主要涉及到 or and not between in like > < = !> !< != <> () <= >= is null is not null

  --查詢cl_id 大於 1 的所有資訊

  select * from class where cl_id>1

  --使用 or

  select * from class where cl_id<>10 or cl_class='百傑一班'

  --使用and

  select * from class where cl_id<>10 and cl_class='百傑一班'

  --使用like 和 %

  select * from class where cl_class like '百傑%'

  select * from class where cl_remark like '%上午%'

  --使用 between

  select * from class where cl_id between 3 and 5

  --使用 between 配合上 not

  select * from class where cl_id not between 3 and 5

  --使用 is not null

  select * from class where cl_remark is not null

  --使用 in

  select * from class where cl_class in('千星一班','百傑二班')

  

--=================使用數學運算子======================

 

首頁 1 2 末頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.