天天看點

Mysql中的一些基本操作

首先在指令行啟動mysql

Mysql中的一些基本操作

回車輸入密碼,mysql登陸成功:

Mysql中的一些基本操作

1.檢視已有資料庫名稱:

輸入指令show data base; 顯示已有資料庫

Mysql中的一些基本操作

2.建立資料庫:

輸入 create database blog_data(資料庫名稱);

再次輸入show databases;檢視新資料庫是否建立成功;

Mysql中的一些基本操作

圖中可以看到,剛剛建立的名為blog_data的資料庫已經建立成功。

3.選擇一個資料庫,并在其中建立資料表:

輸入 use blog_data(資料庫名稱);進入指定的資料庫。

輸入show tables; 檢視資料庫中有什麼表,剛剛建立的資料庫裡邊表為空。

輸入create table test_data(表名)(name varchar(20),

phonenum varchar(11),

email varchar(30),

id int not null auto_increment,

primary key(id));

建立名為test_data的資料表;并将id設為主鍵,自增長;

再次輸入show tables;檢視是否建立成功。

Mysql中的一些基本操作

4.顯示表結構,并在表中添加記錄:

  1. 輸入desc test_data;顯示表結構。 輸入select *from

    test_data;可以檢視表中的資料,剛剛建的表裡邊沒有資料,會顯示資料為空;

  2. 輸入insert into test_data values(“資料1”,”資料2”,”資料3”,資料4);插入的資料要和表中字段的屬性比對,字元型加“”整型直接輸入資料,表中有多少個字段就輸入多少個數值;
Mysql中的一些基本操作

再輸入select * from test_data;可以檢視到剛剛插入到表中的資料;

Mysql中的一些基本操作

圖中可以看到剛剛插入到表中的資料;

5.修改、删除資料:

輸入update test_data(表名) set phonenum=’11111111111’ where name=”xing”;

Mysql中的一些基本操作

圖中資料對比可以看出,資料修改成功;

輸入delete from test_data where(name=”xing”);

Mysql中的一些基本操作

資料删除成功;

6.删除、添加表中字段,修改字段預設值:

向表中添加字段address

alter table test_data add column address varchar(20) default”“;

Mysql中的一些基本操作

更改表中字段名稱和屬性:

Mysql中的一些基本操作

更改表中字段的預設值:

alter table test_data alter address set default “shenzhen”;

Mysql中的一些基本操作

删除表中字段alter table test_data drop column email;

Mysql中的一些基本操作

删除資料表 drop table test_data;

Mysql中的一些基本操作

删除資料庫 drop database blog_data;

Mysql中的一些基本操作

mysql資料庫基本操作暫時blog這些,後續用到後繼續更新~~

轉載于:https://www.cnblogs.com/xpfei/p/7450809.html