MySQL資料庫基本操作,mysql基本操作

來源:互聯網
上載者:User

MySQL資料庫基本操作,mysql基本操作
轉載請表明出處:http://blog.csdn.net/u012637501(嵌入式_小J的天空)
一、MySQL資料庫建立1.建立庫與表■建立庫語句:create database [庫名稱];■查庫庫語句:show databases;■選擇庫語句:use [庫名稱];■建立表語句:create table [表名稱];■查看錶語句:desc [表名稱];執行個體:假設一個公司為一個庫,在庫中建立一個員工表,用於儲存員工的詳細資料。

id name age email tel salary riqi
1 林俊傑 32 linjunjie@qq.com 18819463269 10000 2015-03-23
2 羅志祥 33 luozhixiang@qq.com 18819463211 9000 2015-03-01
(1)建立一個公司庫create database accompany;show databases;use accompany;
(2)向庫中建立一張員工表create table employee(    id int primary key auto_increment,    name char(3) not null default '',    age tinyint not null default 0,    email varchar(30) not null default '',    tel char(11) not null default '',    salary decimal(7,2) not null default 5000.55,    riqi date not null default '2015-1-1'    )charset utf8;    由可知,建立一張表實質上是對錶中列的聲明。資料是以檔案的形式放在硬碟(也有放在記憶體裡的) 。在聲明列時,不同的列類型占的空間不一樣 ,選列的的原則是夠用,又不浪費且推薦聲明預設值(not null default ())。★primary key auto_increment:為id列的約束條件,用於設定為該表的主鍵;★char(M)或varchar(M):列類型,M表示佔用的字元數;★decimal(M,D):decimal(M,D) :M表示精度(總位元,不包含點);D表示標度(小數位元)
二、表的增刪改查 1.添加/修改/刪除表資料■增加一行資料:(思想:往哪張表添加?給哪幾行添加值?分別是什麼值?) insert  into  表名  (列1,列2,...,列n)   values  (值1,值2...,值n);   註:當不標明(列1,...,列n)時,預設插入所有列。■修改某行的列資料:(思想:改哪張表?你需要改幾列的值?分別改為什麼值?在哪些行生效?)    update 表名 set     列1=新值1, 列2=新值2,where expr;■刪除行:(思想:你要刪除哪張表的資料?你要刪掉哪些行?)    delete from 表名 where expr;■查看錶資料:select * from [表名];(1)向員工表中增加幾行資料insert into employee (id,name,age,email,tel,salary,riqi)values(1,'林俊傑',32,'linjunjie@qq.com',18819463269,10000.00,'2015-03-25');(2)刪除/修改資料delete from employee where id=2;set names gbk;update employee set name='林俊傑' where id=1;注意:由於我們在建立表時,指定字元編碼為utf8。倘若需要輸入中文字元,則需要更改字元編碼為gbk,否者就會出現"Incorrect string value:'xC1\xD6\xBF.."錯誤。
2.修改欄位及屬性■修改表(表中的欄位類型、欄位名、添加欄位、刪除欄位、修改表名)①修改表中欄位屬性(不能修改欄位名)        alter table [表名] modify [欄位名] [欄位類型] [約束條件] [fisrt|after 列名];②修改表中欄位名及屬性        alter table [表名] change [源欄位名] [修改後的欄位名] [欄位類型] [約束條件] [fisrt|after 列名];③增加表欄位         alter table [表名] add [欄位名] [欄位類型] [約束條件] [first|after 列名];④刪除表欄位        alter table [表名] drop [欄位名];注意:[first|after 列名],用於修改欄位的排序,其中,after將新增的欄位添加在某一欄位後;first表示將建立的欄位放在該表第一列。■修改表名命令:alter table [表名] rename to [新表名]; (1)修改欄位(列)名稱及屬性alter table employee modify name char(6) not null default '';

3.查詢表■查詢表列資料(思想:查那張表?你要選擇那些列來查詢?要選擇哪些行?)    select 列1,列2,...,列n from 表名 where exper;註:exper運算式用於指明查詢哪(些)行的列(列1,列2,...,列n)資料

聯繫我們

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