mysql增刪改查操作集錦
阿新 • • 發佈:2018-12-22
資料庫的操作指令:
- 新增資料庫:
mysql> create datatable database_name;
- 檢視已建立的資料庫:
mysql> show datdabases;
- 選擇資料庫:
mysql> use database_name;
Database changed
- 刪除資料庫:
mysql> drop database database_name;
資料表的操作指令:
- 檢視所有的資料表
mysql> show tables;
- 新增資料表:pytable1
mysql> create table pytable1 (id int(7),name char(30),salary bigint(10),age int(3)); Query OK, 0 rows affected (2.74 sec)
- 修改表名:
mysql> alter table pytable1 rename table2;
- 刪除資料表:
mysql> drop table pytable2;
- 修改表結構: 新增、刪除、修改、重新命名、新增主鍵和索引
mysql> alter table pytable1 add column date datetime; # 新增一列 mysql> alter table pytable1 drop column date; # 刪除一列 mysql> alter table pytable1 change sex gender char(20); # 修改列名;"sex"為舊的列名;"gender"為新的列名 mysql> alter table pytable1 modify name varchar(30) not null; # 更改列的屬性 mysql> alter table pytable1 add primary key(id); # 向"id"列新增主鍵 mysql> alter table pytable1 add unique name_unique_index(`name`); #向"name"列新增唯一索引;(`age`)中"`"符號是"esc下面的鍵" mysql> alter table pytable1 add index age_index(`age`); # 向"age"列新增普通索引 mysql> alter table pytable1 add column school char(10) after birthday; # 定義新增列的位置
- 查看錶結構
mysql> desc pytable1; +----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+-------+ | id | int(7) | NO | PRI | 0 | | | name | varchar(30) | NO | UNI | NULL | | | salary | bigint(10) | YES | | NULL | | | age | int(3) | YES | MUL | NULL | | | birthday | date | YES | | NULL | | | gender | char(20) | YES | | NULL | | | city | varchar(30) | YES | | NULL | | | school | char(10) | YES | | NULL | | +----------+-------------+------+-----+---------+-------+ 8 rows in set (0.01 sec)
- 刪除表的一列:
mysql> alter table pytable1 drop column school;
- 向表中新增內容:
mysql> insert into pytable1 values (1001,'Belle',8000,25,'female',19930526,'深圳'); # 插入所有列的資料
mysql> insert into pytable1 (id,name,age,salary) values ('1002','Bob','28','9000'); # 插入部分列的資料
- 查詢表中的所有資料:
mysql> select * from pytable1;
+------+-------+--------+------+--------+------------+--------+
| id | name | salary | age | gender | birthday | city |
+------+-------+--------+------+--------+------------+--------+
| 1001 | Belle | 8000 | 25 | female | 1993-05-26 | 深圳 |
| 1002 | Bob | 9000 | 28 | NULL | NULL | NULL |
+------+-------+--------+------+--------+------------+--------+
2 rows in set (0.00 sec)