mysql資料庫和資料庫表操作語句學習筆記
mysql資料庫和資料庫表操作語句學習筆記
資料庫操作語句
1.建立資料庫
creat database ‘資料庫名’
mysql> CREATE DATABASE test;
Query OK, 1 row affected (0.00 sec)
不過預設建立的資料庫編碼格式為latin1,在mysql中儲存和顯示中文時會產生亂碼
查詢資料庫編碼格式 mysql> show variables like 'character_set_database'; +------------------------+--------+ | Variable_name | Value | +------------------------+--------+ | character_set_database | latin1 | +------------------------+--------+ 1 row in set (0.00 sec)
我們可以在建立資料庫的時候指定編碼格式
例如:
mysql> CREATE DATABASE test1 charset=utf8mb4;
Query OK, 1 row affected (0.00 sec)
另外資料庫不能重複建立,會報錯(ERROR 1007 (HY000): Can’t create database ‘test’; database exists)
2.檢視資料庫
mysql> show databases; 用來檢視所有資料庫 +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | | test1 | +--------------------+ 6 rows in set (0.00 sec)
3.切換資料庫
選擇指定的資料庫
use
例如:
mysql> use test1;
Database changed
執行以上命令後,就選擇了test1這個資料庫,後續的操作都會在test1資料庫中進行操作
4.刪除資料庫
DROP DATABASE 語句用於刪除資料庫
mysql> drop database test; Query OK, 0 rows affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test1 | +--------------------+ 5 rows in set (0.00 sec)
5.修改資料庫
只能修改字符集
mysql> alter database test charset=utf8mb4;
Query OK, 1 row affected (0.00 sec)
表的操作語句
1.建立表
表由行和列組成,每個表都必須有個表名
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
column_name3 data_type(size),
....
);
column_name 引數規定表中列的名稱。
data_type 引數規定列的資料型別(例如 varchar、integer、decimal、date 等等)。
size 引數規定表中列的最大長度。
例如:
建立一個學生表,包含三列,ID,姓名,年齡
在建立表之前要先進入到相應的資料庫中,在資料庫中建立表
mysql> use test1;
Database changed
mysql> show tables;
Empty set (0.00 sec)
mysql> create table student (id int ,name varchar(128),age int);
Query OK, 0 rows affected (0.02 sec)
id列和age列的資料型別是int,包含整數,name列的資料型別是varchar,包含字元
2.查詢表
mysql> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| student |
+-----------------+
1 row in set (0.00 sec)
3.刪除表
DROP TABLE table_name
mysql> drop table student;
Query OK, 0 rows affected (0.03 sec)
mysql> show tables;
Empty set (0.00 sec)
補充:如果只刪除表內的資料,不刪除表本身,使用TRUNCATE TABLE語句
mysql> truncate table student;
Query OK, 0 rows affected (0.01 sec)
4.查詢表結構的詳細資訊
describe(或者desc)
mysql> describe student;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(128) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
5.查詢建表語句
show create table
mysql> show create table student;
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
`id` int(11) DEFAULT NULL,
`name` varchar(128) DEFAULT NULL,
`age` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 |
+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
6.修改表
alter table 原表名 rename 新表名
mysql> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| student |
+-----------------+
1 row in set (0.00 sec)
mysql> alter table student rename teacher;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| teacher |
+-----------------+
1 row in set (0.00 sec)
也可以將表移動到另一個數據庫中
alter table rename to newdatabase.newtablename;
例如:將test1中的teacher表移動到test庫中 並取名為student
mysql> alter table teacher rename to test.student;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
Empty set (0.00 sec)
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
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| student |
+----------------+
1 row in set (0.00 sec)
7.修改表字段
增加一個新欄位
alter table add 欄位名 資料型別 [屬性] ;
例:新增一個分數字段
mysql> alter table student add score float;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
增加一個新欄位,並放在首位
alter table add 欄位名 資料型別 [屬性] first ;
例:新增一個class欄位,並放到第一位
mysql> alter table student add class int first ;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe student;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| class | int(11) | YES | | NULL | |
| id | int(11) | YES | | NULL | |
| name | varchar(128) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| score | float | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
新增一個欄位,並放到指定欄位後面
alter table add 欄位名 資料型別 [屬性] after 指定欄位 ;
例:加一個aaa欄位 放到age後面
mysql> alter table student add aaa int after age ;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe student;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| class | int(11) | YES | | NULL | |
| id | int(11) | YES | | NULL | |
| name | varchar(128) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| aaa | int(11) | YES | | NULL | |
| score | float | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
修改欄位屬性
alter table modify 欄位名 資料型別 [屬性] ;
例:將原本class的int型別 改為varchar屬性 並限制最大長度為12
mysql> alter table student modify class varchar(12) ;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe student;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| class | varchar(12) | YES | | NULL | |
| id | int(11) | YES | | NULL | |
| name | varchar(128) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| aaa | int(11) | YES | | NULL | |
| score | float | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
修改欄位名
alter table change 原欄位名 新欄位名 資料型別 [屬性] ;
例:將aaa欄位改為 bbb欄位
mysql> alter table student change aaa bbb int;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe student;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| class | varchar(12) | YES | | NULL | |
| id | int(11) | YES | | NULL | |
| name | varchar(128) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| bbb | int(11) | YES | | NULL | |
| score | float | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
修改欄位位置
alter table change 原欄位名 新欄位名 資料型別 [屬性] after 指定欄位;
例:將bbb欄位改為ccc 並放到name後面
mysql> alter table student change bbb ccc int after name;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe student;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| class | varchar(12) | YES | | NULL | |
| id | int(11) | YES | | NULL | |
| name | varchar(128) | YES | | NULL | |
| ccc | int(11) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| score | float | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
刪除欄位
alter table drop <欄位>
例:刪除ccc欄位
mysql> alter table student drop ccc;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> describe student;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| class | varchar(12) | YES | | NULL | |
| id | int(11) | YES | | NULL | |
| name | varchar(128) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| score | float | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)