mysql 的基本操作總結--增刪改查

來源:互聯網
上載者:User

標籤:value   視窗   結構   總結   插入   建議   資料   host   pre   

本文只是總結一下mysql 的基本操作,增刪改查,以便忘記的時候可以查詢一下

 

1.建立資料庫

文法:CREATE DATABASES 資料庫名;例子: CREATE DATABASES student;

 

2.刪除資料庫

文法:drop database 資料庫名;例子:drop database student;

 

3:設定mysql編碼:(在cmd視窗查詢)

建議:每次登陸mysql是執行該語句

set name gbk

 

4.建立資料表 

//文法:CREATE TABLE 資料表名( 欄位名1 資料類型(長度) unsigned(可填), // ( unsigned/ 無負值)欄位名2 資料類型(長度) 限制條件, ····· //最後一個不需要用逗號 );//例子:   create table student(id int(20) primary key,name varchar(11),grade float(20))


5.複製已知表的結構

文法: create table 新表名 like 舊錶名例子: create table studens_new like student;

 

6.資料表插入資料

  6.1使用INSERT 關鍵字為資料表插入資料

//文法:insert into 資料表名(欄位名1,欄位名2,···) values(值1,值2,····),values(值1,值2,····); //例子:insert into student(id,name)values(1,‘小明‘),values(2,‘張三‘);//或者 insert into student values(1,‘小明‘),(2,‘張三‘);

 

  6.2.使用INSERT的SET關鍵字為資料表插入資料

文法:insert into 資料表名 set 欄位1=值1,欄位2=值2.....;例子:insert into student set id=2,name=‘小明‘;

 

7.更新資料
使用UPDATE ...SET關鍵字更新資料

//文法:UPDATE 表名 set 欄位1=值1,欄位2=值2....where 條件運算式;//例子:(1)UPDATE student set grade=60,name=‘小明‘ where id=1;(更新部分資料)(2)UPDATE student set grade=100;(更新全部資料)

 

8.刪除資料

使用delete關鍵字刪除資料

//文法:delete from 表名 where 條件限制;//例子:(1)delete from student where id=2;(刪除部分資料)(2)delete from student;(刪除全部資料)

 

9.使用truncate關鍵字刪除資料

//文法:truncate from 表名 ;//例子:truncate from student;

 

10.使用DROP關鍵字刪除資料表

//文法:drop table 表名;//例子:drop table student;

 

11.使用alter...rename關鍵字修改資料表名(RENAME)

//文法:alter table 舊錶名 rename to 新表名;//例子:alter table student rename to students;

 

12.使用 alter ...modify關鍵字修改欄位的資料類型(MODIFY )

//文法:alter table 表名 MODIFY 欄位名 要修改的資料類型;//例子:alter table student modify id int(20);

 

13.使用 alter...add 關鍵字為資料表添加欄位(ADD)

//文法:alter table 表名 ADD 新欄位名 資料類型() 約束條件 first/after 已存在的欄位;//例子:(1)alter table student add sex varchar(11) first;(2)alter table student add sex varchar(11) after id;

 

14.使用alter...drop 關鍵字刪除欄位(DROP)

//文法:alter table 表名 drop 欄位名,drop 欄位名...;//例子:(1)alter talbe student drop grade;(2)alter table 表名 add sex first,drop grade ; (添加sex欄位在第一,同時刪除grade欄位)

 

 

15.使用AS關鍵字為資料表或者欄位取別名(並沒有修改表名)

//為資料表取別名文法:select * from student as 別名;例子:select * from student as ‘學生表‘;
//為欄位取別名(並沒有修改欄位名)文法:select 欄位1 as 別名1,欄位2 as 別名2... from 表名;例子:select id as ‘學號‘,name as ‘姓名‘ from student;

 

16.從系統(cmd)進入資料庫:

mysql -hlocalhost -uroot -p

 

17. 開啟資料庫

 

//文法:USE 資料庫名;//例子:use student;

 

 

 

 

18.檢查當前開啟的是哪個資料庫或者是否已經成功開啟資料庫

select database();

 

19.查看當前資料庫中的所有資料表

show tables; 

 

20.指定查看當前資料庫中某一個資料表

文法:show table 資料表名; 例子:show table student;

 

21.查看MySQL中所有的資料表

文法:show tables from mysql;

 

22. 查看資料表的結構

//文法:(1) show columns from 資料表名; (2) desc 資料表名;//例子:(1) show columns from student;(2) desc 

 


23. 查看資料表的所有資料/記錄

文法:select*from 資料表名; 例子:select *from student;

 

9.查看資料表的索引

文法:show indexes from 資料表名;例子:show idexes from student;

 

 

 



 

mysql 的基本操作總結--增刪改查

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.