SQL 查詢分析器操作(修改、添加、刪除)表及欄位等

來源:互聯網
上載者:User

一、庫操作
1、、建立資料庫
命令:create database <資料庫名>
例如:建立一個名為xhkdb的資料庫
mysql> create database xhkdb;
2、顯示所有的資料庫
命令:show databases (注意:最後有個s)
mysql> show databases;
3、刪除資料庫
命令:drop database <資料庫名>
例如:刪除名為 xhkdb的資料庫
mysql> drop database xhkdb;
4、串連資料庫
命令: use <資料庫名>
例如:如果xhkdb資料庫存在,嘗試存取它:
mysql> use xhkdb;
工具提示:Database changed
5、查看當前使用的資料庫
mysql> select database();

6、當前資料庫包含的表資訊:
mysql> show tables; (注意:最後有個s)

 

二、表操作,操作之前應串連某個資料庫
1、建表

命令:create table <表名> ( <欄位名1> <類型1> [,..<欄位名n> <類型n>]);

mysql> create table MyClass(
> id int(4) not null primary key auto_increment,
> name char(20) not null,
> sex int(4) not null default '0',
> degree double(16,2));
2、擷取表結構
命令: desc 表名,或者show columns from 表名
mysql>DESCRIBE MyClass
mysql> desc MyClass;
mysql> show columns from MyClass;
3、刪除表
命令:drop table <表名>
例如:刪除表名為 MyClass 的表
mysql> drop table MyClass;
4、插入資料
命令:insert into <表名> [( <欄位名1>[,..<欄位名n > ])] values ( 值1 )[, ( 值n )]
例如,往表 MyClass中插入二條記錄, 這二條記錄表示:編號為1的名為Tom的成績為96.45, 編號為2 的名為Joan 的成績為82.99,編號為3 的名為Wang 的成績為96.5.
mysql> insert into MyClass values(1,'Tom',96.45),(2,'Joan',82.99), (2,'Wang', 96.59);
5、查詢表中的資料
1)、查詢所有行

命令: select <欄位1,欄位2,...> from < 表名 > where < 運算式 >
例如:查看錶 MyClass 中所有資料
mysql> select * from MyClass;
2)、查詢前幾行資料
例如:查看錶 MyClass 中前2行資料
mysql> select * from MyClass order by id limit 0,2;

或者:

mysql> select * from MyClass limit 0,2;
6、刪除表中資料
命令:delete from 表名 where 運算式
例如:刪除表 MyClass中編號為1 的記錄
mysql> delete from MyClass where id=1;
7、修改表中資料:

update 表名 set 欄位=新值,… where 條件
mysql> update MyClass set name='Mary' where id=1;
8、在表中增加欄位:

命令:alter table 表名 add欄位 類型 其他;
例如:在表MyClass中添加了一個欄位passtest,類型為int(4),預設值為0
mysql> alter table MyClass add passtest int(4) default '0'

9.修改某個表的欄位名字和類型及指定為空白或非空

1>alter table 表名稱 change 欄位名稱 欄位名稱 欄位類型 [是否允許非空];
2>alter table 表名稱 modify 欄位名稱 欄位類型 [是否允許非空];

10、更改表名:
命令:rename table 原表名 to 新表名;
例如:在表MyClass名字更改為YouClass
mysql> rename table MyClass to YouClass;

更新欄位內容
update 表名 set 欄位名 = 新內容
update 表名 set 欄位名 = replace(欄位名,'舊內容','新內容');

文章前面加入4個空格
update article set content=concat('  ',content);

欄位類型
1.INT[(M)] 型: 正常大小整數類型
2.DOUBLE[(M,D)] [ZEROFILL] 型: 正常大小(雙精密)浮點數字類型
3.DATE 日期類型:支援的範圍是1000-01-01到9999-12-31。MySQL以YYYY-MM-DD格式來顯示DATE值,但是允許你使用字串或數字把值賦給DATE列
4.CHAR(M) 型:定長字串類型,當儲存時,總是是用空格填滿右邊到指定的長度
5.BLOB TEXT類型,最大長度為65535(2^16-1)個字元。
6.VARCHAR型:變長字串類型

 

文章來源:http://www.cnblogs.com/hateislove214/archive/2010/11/05/1869889.html

 

聯繫我們

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