1.1 創(chuàng)建數(shù)據(jù)庫
-- 創(chuàng)建一個名為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)建兩個數(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> -- 記錄班級信息的數(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)建上述兩個數(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不二雄班");