數(shù)據(jù)庫查基本操作

1.1 創(chuàng)建數(shù)據(jù)庫

-- 創(chuàng)建一個(gè)名為test的數(shù)據(jù)庫,并制定為utf-8編碼;

-- 注意:charset后應(yīng)該寫utf8而不是utf-8。

create database test charset=utf8;

1.2 使用數(shù)據(jù)庫

mysql> use test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

1.3 顯示當(dāng)前使用的數(shù)據(jù)庫

mysql> select database();

+------------+

| database() |

+------------+

| test? ? ? |

+------------+

1 row in set (0.00 sec)

1.4 創(chuàng)建兩個(gè)數(shù)據(jù)表

mysql> -- 記錄學(xué)生信息的數(shù)據(jù)表students

mysql> create table students(

-> id int unsigned primary key auto_increment not null,

-> name varchar(20) default "",

-> age tinyint unsigned default 0,

-> height decimal(5,2),

-> gender enum("男", "女", "中性", "保密") default "保密",

-> cls_id int unsigned default 0,

-> is_delete bit default 0

-> );

Query OK, 0 rows affected (0.03 sec)

mysql> -- 記錄班級(jí)信息的數(shù)據(jù)表classes

mysql> create table classes(

-> id int unsigned auto_increment primary key not null,

-> name varchar(30) not null

-> );

Query OK, 0 rows affected (0.01 sec)

在終端輸入創(chuàng)建上述兩個(gè)數(shù)據(jù)表的命令后,可通過show tables;語句顯示是否創(chuàng)建成功。

mysql> show tables;

+----------------+

| Tables_in_test |

+----------------+

| classes? ? ? ? |

| students? ? ? |

+----------------+

2 rows in set (0.00 sec)

1.5 向數(shù)據(jù)表插入數(shù)據(jù)

-- 向數(shù)據(jù)表students中插入14條記錄

insert into students values

(0, "小明", 18, 188.88, 2, 1, 0),

(0, "小華", 18, 180.00, 2, 2, 0),

(0, "彭于晏", 29, 185.00, 1, 1, 0),

(0, "劉德華", 59, 175.00, 1, 2, 1),

(0, "黃蓉", 38, 160.00, 2, 1, 0),

(0, "鳳姐", 28, 150.00, 4, 1, 0),

(0, "王祖賢", 18, 172.00, 2, 1, 1),

(0, "周杰倫", 36, NULL, 1, 1, 0),

(0, "陳坤", 27, 181.00, 1, 2, 0),

(0, "劉亦菲", 25, 166.00, 2, 2, 0),

(0, "金星", 33, 162.00, 3, 3, 1),

(0, "靜香", 12, 180.00, 2, 4, 0),

(0, "郭靖", 12, 170.00, 1, 4, 0),

(0, "周杰", 34, 176.00, 2, 5, 0);

-- 向數(shù)據(jù)表classes中插入兩條記錄

insert into classes values(0, "金庸武俠班"), (0, "藤子F不二雄班");

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容