數據庫基本操作命令


MySQLl數據庫基礎知識:

MySQL有三大類數據類型, 分別為數字、日期\時間、字符串, 這三大類中又更細致的划分了許多子類型:

  • 數字類型
    • 整數: tinyint、smallint、mediumint、int、bigint
    • 浮點數: float、double、real、decimal
  • 日期和時間: date、time、datetime、timestamp、year
  • 字符串類型
    • 字符串: char、varchar
    • 文本: tinytext、text、mediumtext、longtext
    • 二進制(可用來存儲圖片、音樂等): tinyblob、blob、mediumblob、longblob

  這里不能詳細對這些類型進行介紹了, 篇幅可能會很長, 詳細介紹參見:

http://www.cnblogs.com/zbseoag/archive/2013/03/19/2970004.html

1、查看數據庫

2、切換數據庫

3、查看數據庫中已創建的表

4、查看表的相關信息

5、創建表

使用 create table 語句可完成對表的創建, create table 的常見形式:

create table 表名稱(列聲明);

以創建 students 表為例, 表中將存放 學號(id)、姓名(name)、性別(sex)、年齡(age)、聯系電話(tel) 這些內容:

create table students
	(
		id int unsigned not null auto_increment primary key,
		name char(8) not null,
		sex char(4) not null,
		age tinyint unsigned not null,
		tel char(13) null default "-"
	);

create table tablename(columns) 為創建數據庫表的命令, 列的名稱以及該列的數據類型將在括號內完成;

括號內聲明了5列內容, id、name、sex、age、tel為每列的名稱, 后面跟的是數據類型描述, 列與列的描述之間用逗號(,)隔開;

以 "id int unsigned not null auto_increment primary key" 行進行介紹:

  • "id" 為列的名稱;
  • "int" 指定該列的類型為 int(取值范圍為 -8388608到8388607), 在后面我們又用 "unsigned" 加以修飾, 表示該類型為無符號型, 此時該列的取值范圍為 0到16777215;
  • "not null" 說明該列的值不能為空, 必須要填, 如果不指定該屬性, 默認可為空;
  • "auto_increment" 需在整數列中使用, 其作用是在插入數據時若該列為 NULL, MySQL將自動產生一個比現存值更大的唯一標識符值。在每張表中僅能有一個這樣的值且所在列必須為索引列。
  • "primary key" 表示該列是表的主鍵, 本列的值必須唯一, MySQL將自動索引該列。

下面的 char(8) 表示存儲的字符長度為8, tinyint的取值范圍為 -127到128, default 屬性指定當該列值為空時的默認值。

6、向表中插入數據

7、向表stu中增加一列數據sex

8、將列sex移動到列name之后

將列sex移動到列name之前

9、更新表中信息

10、查詢表中信息

按特定條件查詢:

where 關鍵詞用於指定查詢條件, 用法形式為: select 列名稱 from 表名稱 where 條件;

以查詢所有性別為女的信息為例, 輸入查詢語句: select * from students where sex="女";

where 子句不僅僅支持 "where 列名 = 值" 這種名等於值的查詢形式, 對一般的比較運算的運算符都是支持的, 例如 =、>、<、>=、<、!= 以及一些擴展運算符 is [not] null、in、like 等等。 還可以對查詢條件使用 or 和 and 進行組合查詢, 以后還會學到更加高級的條件查詢方式, 這里不再多做介紹。

示例:

select * from students where age > 21;       #查詢年齡在21歲以上的所有人信息: 

select * from students where name like "%王%";  #查詢名字中帶有 "王" 字的所有人信息:

select * from students where id<5 and age>20;  #查詢id小於5且年齡大於20的所有人信息:

11、刪除表中數據

delete from 表名稱 where 刪除條件;

12、表的修改

添加列

基本形式: alter table 表名 add 列名 列數據類型 [after 插入位置];

示例:

在表的最后追加列 address: alter table students add address char(60);

在名為 age 的列后插入列 birthday: alter table students add birthday date after age;

修改列

 alter table 表名 change 列名稱 列新名稱 新數據類型;

示例:

將表 tel 列改名為 telphone: alter table students change tel telphone char(13) default "-";

將 name 列的數據類型改為 char(16): alter table students change name name char(16) not null;

alter table 表名 drop 列名稱;     #刪除表中的列
alter table 表名 rename 新表名;   #重命名表
drop table 表名;                 #刪除表

drop database 數據庫名; #刪除數據庫

 

更多MySQL數據庫資料鏈接一下網址:

      http://www.runoob.com/mysql/mysql-tutorial.html

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM