mysql資料庫增、刪、改、查等基本命令

來源:互聯網
上載者:User

標籤:

測試環境:windows7 64位 mysql.exe、Navicat Lite for MySQL、mysql 5.0.18

mysql資料庫的結構:
資料庫(database)包含多個表(table);表包含多個屬性列(column);屬性列包含多個資料(data);屬性列具有四個資訊(field,type,null,key,default)。

我們要做得工作就是對database、table、column、data進行增、刪、改、查四種操作,一共4*4=16種命令。

 

1.1 增 database  --建立一個資料庫
create database databaseName;  --建立名為databaseName的資料庫

例:create database mydb;

 

1.2 刪 database  --刪除一個資料庫
drop database databaseName;  --刪除名為databaseName的資料庫

drop database if exists databaseName;  --如果存在資料庫databaseName,則刪除

 

1.3 改 database

未知

 

1.4 查 database
show databases;  --顯示所有database的databaseName

select database();  --顯示當前database的databaseName

 

2.1 增 table    --建立一個表,建立表時至少有一個column,否則報錯
create table tableName
(
columnName1 <屬性資訊>
[,primary key(columnName)]  --也可在此定義主鍵(primary key),[]中內容可有可無
[,foreign key( columnName1 ) reference tableName2(columnName2)]/*定義外鍵columnName1是當前表已經定義的column,tableName2是另一個已經定義的表名,columnName2是另一個表   的column名字*/
)[comment[=]‘table的描述內容‘];
--<屬性資訊>:typeName[null|not null][default defaultValue] [primary key][auto_increment][comment ‘column的描述內容‘][identity(起始值,遞增量)]
--null|not null:空或非空
--default defaultValue:預設值
--auto_increment:從1或當前最大值開始自動增加1,與identity(1,1)一樣

例:

create table mytable

(

  id   int(8)   not null   auto_increment   comment ‘表的主鍵‘   primary key,

  name char(10)   not null   default ‘無‘,

  foreign key(id) reference mytable1(id_1)

)comment=‘我的表‘;

 

2.2 刪 table--刪除表
drop table tableName1 [,tableName2]--可刪除一個或多個表,預設為當前資料庫中的表
drop table databaseName.tableName--刪除databaseName中名為tableName的表

 

2.3 改 table  --改變表的名字
rename table tableName to tableName_new [,tableName1 to tableName_new1];--將名為tableName的表重新命名為tableName_new
alter table tableName rename to tableName_new;--同上

 

2.4 查 table  --查看表的名字
show tables from databaseName;  --顯示databaseName的所有tableName
show tables;  --顯示當前database的所有tableName(use databaseName;--進入某個資料庫)

 

3.1 增 column
alter table tableName add [column] columnName <屬性資訊>;  --插入columnName,<屬性資訊>參考2.1

例:alter table mytable add age int(2) default 0;

alter table tableName add [column] columnName <屬性資訊> after columnName1;  --在columnName1後插入columnName

 

3.2 刪 column
alter table tableName drop [column] columnName;  --刪除columnName

 

3.3 改 column
alter table tableName modify columnName <屬性資訊>;  --修改columnName的<屬性資訊>

例:alter table mytable modify age int(4);

alter table tableName change columnName columnName_new <屬性資訊>;   --修改columnName的名字為columnName_new及其<屬性資訊>

 

3.4 查 column--查看錶中所有column的columnName及其資訊
describe tableName;--查看錶名為tableName中的所有column的columnName及其<屬性資訊>
show columns from tableName;--同上

 

4.1 增 data

insert into tableName(columnName1,columnName2[,columnName3])
values(value1,value2[,value3])[,(value_1,value_2[,value_3])];--插入一行或多行資料

例:insert into mytable (name,age) values(‘Bob‘,20),(‘Linda‘,21);

 

4.2 刪 data

truncate table tableName;    --清空tableName中的資料

delete from tableName;      --同上

delete from tableName where <條件陳述式>;  --刪除滿足<條件陳述式>的資料行

例:delete from mytable where age=20;

 

4.3 改 data

update tableName set <columnName=data> where <條件陳述式>;  --改變滿足<條件陳述式>的行中columnName的資料為data

例:update mytable set age=22 where name=‘Bob‘;

 

4.4 查 data

select columnName1[,columnName2] from tableName where <條件陳述式>;  --尋找滿足<條件陳述式>的屬性名稱為columnName1[,columnName2]的資料

例:select name,age from mytable where id=2;

select * from tableName where <條件陳述式>;  --尋找滿足<條件陳述式>的資料行

 

參考:http://www.w3school.com.cn/sql/index.asp

   http://blog.csdn.net/Sunboy_2050/article/details/5131863

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.