MySQL資料庫 資料庫 表 資料常用操作

2021-09-25 03:18:00 字數 3920 閱讀 5272

建立資料庫:

create database 資料庫名;

顯示所有可訪問資料庫:

show databases;

顯示當前選中的資料庫:

select database();

刪除資料庫:

drop database 資料庫名;

建立表:

create table table_name(

id int not null auto_increment primary key,

name char(10),

age int,

adr varchar(20)

刪除表:

drop table table_name;

檢視表結構:

desc table_name;

修改表:

1)修改屬性

alter table table_name modify name char(80) unique;

2)增加屬性

alter table table_name add *** char(10);

3)刪除屬性

alter table table_name drop ***;

4)更改屬性名

alter table table_name change adr adress varchar(50);     #舊名       新名   

5)修改表名

alter table table_name rename table_name2;

6)增加索引

alter table table_name2 add index(name);

7)刪除索引

alter table table_name drop index name;

8)檢視索引

show index from table_name2;

1)insert into table_name2 values(....),(.....);

2)insert into table_name2(屬性1,...)  values(值1);       #有空值的只能用這種方式新增

delete from table_name2 where 條件;

ps:不加條件則全部刪除

update table_name2 set age=20,***='boy' where id=1,

ps:不加條件則全部刪除

1)條件查詢

①普通條件查詢 

select * from table_name2 where id >=2 and name='lili';     #查詢table_name2表中 id>=2name為 lili 的記錄的所有屬性

② 模糊查詢

_代表乙個字元,%代表任意字元

select * from table_name2 where name like '_';     

select * from table_name2 where name like 'm%';

③範圍查詢

select * from table_name2 where id between 2 and 4;   #查詢id在[2,4]區間內的記錄,連續

select * from table_name2 where id in (4,6);                  #查詢id在集合(4,6)中的記錄,不連續

④非空/空值查詢

select * from table_name2 where adr is null;         #查詢adr為空的記錄

select * from table_name2 where adr is not null;  #查詢adr不為空的記錄

ps:優先順序:小括號》not/is>比較運算子》and>or

2)聚合

①計數select count(*) from table_name2;                #總記錄數

select count(adress) from table_name2;      #adress不為空的記錄數

②最大值、最小值、求和、平均值(語法一樣)

select max(age) from table_name2;

3)分組

select ***,sum(age) from table_name2 group by ***;    #對年齡按性別進行分組求和

4)排序

select * from table_name2 order by age;            #按年齡公升序

select * from table_name2 order by age desc;   #按年齡降序

5)分頁

語法:select * from table_name2 limit 開始行號   行數;      #行號從0開始

如:select * from table_name2 limit 2,4;                             #從行號為2開始,顯示4行

6)關聯

兩種建立外來鍵約束的方式(乙個表中的 foreign key 指向另乙個表中的 unique key(唯一約束的鍵)):

檢視外來鍵約束:show create table table_name2;

①建立表之後建立外來鍵約束

語法:alter table 從表名 add foreign key(外來鍵字段) references 主表名(主表字段);

alter table table_name2 add foreign key(id) references tablename0(main_id);

②建立表時建立外來鍵約束

create table table_name(

id int not null auto_increment primary key,

name char(10),

age varchar(20),

num_id int,

foreign key(num_id) references 主表名(主鍵));

刪除外來鍵約束:

關聯查詢:

select * from 主表名 inner join table_name on  num_id=主鍵;

select * from table_name,主表名 where num_id=主鍵;            #效果和關聯查詢一樣,但是考慮到效能,一般用關聯查詢

多表關聯:

select a.* from a inner join b on a.id=b.id inner join c on a.id=c.id;

ps:

1、where條件寫在關聯之後,多條件用and連線;

2、a left join b    //顯示邊(也就是a)所有的記錄,即使b中無對應資料

3、設定外來鍵約束時,主表的對應字段必須包含從表對應欄位中已有的值;(只已有資料的時候???)

MYSQL資料庫之建立資料庫表

每個表都應有乙個主鍵字段。主鍵用於對錶中的行進行唯一標識。每個主鍵值在表中必須是唯一的。此外,主鍵字段不能為空,這是由於資料庫引擎需要乙個值來對記錄進行定位。主鍵字段永遠要被編入索引。這條規則沒有例外。你必須對主鍵字段進行索引,這樣資料庫引擎才能快速定位給予該鍵值的行。下面的例子把 personid...

mysql資料庫效能資料 MYSQL資料庫效能優化

1.選取最適用的字段屬性 表中字段的寬度設得盡可能小 char 的上限為 255 位元組 固定占用空間 varchar 的上限 65535 位元組 實際占用空間 text 的上限為 65535。盡量把字段設定為 not null,執行查詢的時候,資料庫不用去比較 null 值。2.使用連線 join...

MySQL資料庫 資料庫管理

建立使用者,指定明文密碼 create user rose localhost identified by rosepwd 檢視使用者是否建立成功 select user,host from mysql.user 建立使用者,不設定密碼 create user rose01 localhost se...