MySQL 資料庫基礎(三)(高階操作)

2021-10-17 19:07:39 字數 4045 閱讀 3656

mysql -uroot -p123123

#登入create database class;

將資料表的資料記錄生成到新的表中

方法一:

方法二:

刪除表內的所有資料

方法一:

delete from test02;

#delete清空表後,返回的結果內有刪除的記錄條目;delete工作時是一行一行的刪除記錄資料的

#如果表中有自增長字段,使用delete from 刪除所有記錄後,再次新新增的記錄會從原來最大的記錄 id 後面繼續自增寫入記錄

方法二:

select * from test03;

truncate table test03;

insert into test03 (name,cardid) values (

'wangsan','333333');

select * from test03;

#truncate 清空表後,沒有返回被刪除的條目

#truncate 工作時是將表結構按原樣重新建立,因此在速度上 truncate 會比 delete 清空表快

#使用 truncate table 清空表內資料後,id 會從 1 開始重新記錄

create temporary table 表名 (欄位1 資料型別,欄位2 資料型別[,...]

[,primary key (主鍵名)])

;例:create temporary table test04 (id int not null,name varchar(20) not null,cardid varchar(18) not null unique key,primary key (id))

;show tables;

insert into test04 values (1,'wangsi','444444');

select * from test04;

注意:與外來鍵關聯的主表的字段必須設定為主鍵,要求從表不能是臨時表,且主從表的字段具有相同的資料型別、字元長度和約束

常見的約束

對應主鍵約束

primary key

外來鍵約束

foreign key

非空約束

not null

唯一約束

unique key

預設值約束

default

自增約束

auto_increment

create table test04 (hobid int(4),hobname varchar(50))

;create table test05 (id int(4) primary key auto_increment,name varchar(50),age int(4),hobid int(4))

;alter table test04 add constraint pk_hobid primary key(hobid)

;alter table test05 add constraint fk_hobid foreign key(hobid) references test04(hobid)

;

例:新增資料記錄

insert into test05 values (1,'wangyi','20',1)

;insert into test04 values (1,'wangwu');

insert into test05 values (1,'wangyi',20,1)

;

例:

drop table test04;

drop table test05;

drop table test04;

注:如果要刪除外來鍵約束字段

得先刪除外來鍵約束,再刪除外鍵名

show create table test05;

alter table test05 drop foreign key fk_hobid;

alter table test05 drop key fk_hobid;

desc test05;

oracle資料庫(三) 高階查詢

根據員工的上級編號進行層級關聯 select level empno,ename,mgr from emp connect by prior empno mgr start with mgr is null order bylevel translate expr,from string,to st...

mysql高階操作 MySQL資料庫的高階操作

1.資料備份與還原 1 備份 mysqldump mysqldump u username p password dbname tbname1 tbname2.filename.sql mysqldump u root p mydb2 c mysql dump mydb2 dump.sql 2 恢復...

mysql資料庫高階 mysql資料庫高階

一 索引 索引,是資料庫中專門用於幫助使用者快速查詢資料的一種資料結構。類似於字典中的目錄,查詢字典內容時可以根據目錄查詢到資料的存放位置,然後直接獲取即可。分類 普通索引 唯一索引 全文索引 組合索引 主鍵索引 1 普通索引 普通索引僅有乙個功能 加速查詢 建立表時建立索引 create tabl...