(六)MySQL資料 庫 表的管理

2021-10-02 15:19:33 字數 3694 閱讀 3793

目錄

資料的管理

庫的管理

表的管理

一、資料插入語句1、語法:

insert into 表名(列名,...) values(值1,...);
2、案例:在beauty表中新增一條資訊(13,'張飛','男',1999-01-01,'12345678901',null,2)

use girls;

insert into boys(id,name,***,borndate,phone,photo,boyfriend_id)

values(13,'張飛','男',1999-01-01,'12345678901',null,2);

注意:

①表名後的括號可以省略,若省略,則values後括號內的資訊必須包含該錶的全部字段。

②如果字段允許非空,插入時可以使用null代替非空字段,否則必須插入資料。

④values中的字段順序可以修改,但必須要與表名後括號內欄位對應。

⑤可同時插入多行資料。

二、資料修改語句

1、語法:

update 表名 set phone='要修改的值' where 篩選條件
2、修改單錶內容:

案例:修改beauty表中姓周的女神的**為123456

update beauty b

set b.`phone`='123456'

where b.`name` like '%周%';

3、修改多表內容:

案例:修改張無忌的女朋友的手機號為123456

update beauty b

inner join boys bo on b.`boyfriend_id`=bo.`id`

set b.`phone`='123456'

where bo.`boyname`='張無忌';

注意:update不和from搭配使,而與set搭配使用

三、資料刪除語句

1、語法:

delete from 表名 where 篩選條件;
2、刪除整張表的全部資訊:

#1 delete from beauty;

#2 truncate table beauty;

區別:若被刪表中包含自增長字段,delete刪除表後再插入資料自增長欄位從端點序號開始,truncate從1開始。

3、刪除同一張表中單行/部分行:

案例:刪除編號為3的女生資訊

delete from beauty where beauty.`id`=3;
案例:刪除張無忌的全部女朋友的資訊

delete b,bo

from beauty b

inner join boys bo

on b.`boyfriend_id`=bo.`id`

where bo.`boyname`='張無忌';

注意:多表刪除的語法與單錶刪除有所不同,注意區分。

一、建立庫1、語法:

creat database 【if not exists】庫名
注意:if not exists可以避免報錯

2、案例:建立books庫

create database books;
二、修改庫(庫名)方式一:

rename database 原名稱 to 新名稱;
注意:此語句由於存在漏洞不再使用

方式二:

找到資料庫對應檔案修改檔名

三、刪除庫

drop database if exists books;
一、建立表1、語法:

create table 表名(

欄位名 字段型別,

欄位名 字段型別,

欄位名 字段型別,

...);

2、案例:在books庫中建新錶stu,字段包括(id,name,phone)

use books;

create table stu(

id int,

name varchar(11),

phone int

);

3、檢視表stu_info的資訊

desc stu_info;
執行結果:

二、修改表

1、修改表名:

案例:將表stu重新命名為stu_info

alter table stu rename to stu_info;
2、修改欄位名、型別:

案例:將表stu_info中的字段id重新命名為ids,型別不變

alter table stu_info change column id ids int;
注意:新增、刪除、修改字段、不屬於資料的管理

3、新增字段:

案例:在表stu_info中新增欄位address

alter table stu_info add column address varchar(11);
4、刪除字段:

alter table stu_info drop column address;
5、修改欄位的型別或約束項

alter table book modify column pubdate timestamp;
三、刪除表案例:刪除表stu_infos

drop table stu_infos;
四、表的複製1、僅複製表的結構:

create table stu_infos like stu_info;
2、複製表的結構、資料:

語法:

create table 新錶名

篩選出來的**(部分複製)

案例:複製jobs表中的全部資料到jobbs

use myemployees;

create table jobss

select * from jobs;

複習整理,如有錯誤請指出!

mysql資料庫表管理

複製資料庫 相同mysql伺服器 create table testbackup select from test 相同mysql伺服器,取源表中的某些字段 create table testbackup select field1,field2 from test 不同mysql伺服器 mysql...

MY SQL 管理資料庫和表

一 建立自定義的資料庫 語法如下 create database if notexists default character set 編碼格式 default collate 排序,分組,比較 建庫語句 建立乙個名為demo的資料庫,編碼為utf 8 create database demo de...

管理MySQL資料庫和表

顯示資料庫 show databases 選擇要使用的資料庫 在使用指定資料庫之前,必須通過使用use語句告訴mysql要使用哪個資料庫。use database name 刪除資料庫 drop database語句 drop database if exists database name 在本教...