日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql 原生 添加数据_手撸Mysql原生语句--增删改查

發(fā)布時(shí)間:2023/12/15 数据库 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql 原生 添加数据_手撸Mysql原生语句--增删改查 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

mysql數(shù)據(jù)庫的增刪改查有以下的幾種的情況,

1、DDL語句 數(shù)據(jù)庫定義語言: 數(shù)據(jù)庫、表、視圖、索引、存儲(chǔ)過程,例如CREATE DROP ALTER SHOW

2、DML語句 數(shù)據(jù)庫操縱語言: 插入數(shù)據(jù)INSERT、刪除數(shù)據(jù)DELETE、更新數(shù)據(jù)UPDATE、查詢數(shù)據(jù)SELECT

3、DCL語句 數(shù)據(jù)庫控制語言: 例如控制用戶的訪問權(quán)限GRANT、REVOKE

在這里我們開始手?jǐn)]一下mysql的增刪改查的代碼,視圖、索引等我們之后再次的補(bǔ)充描述。

數(shù)據(jù)庫的增刪改查

1.數(shù)據(jù)庫的增加

create database xxx

2.刪除數(shù)據(jù)庫

drop database xxx

3.改數(shù)據(jù)庫

alter database db1 charset latin1;

4.查詢數(shù)據(jù)庫

show databases;

然后我們看一下表的增刪改查的操作,我們?cè)趯?duì)表進(jìn)行操作的時(shí)候,我們要先指明白要在那個(gè)數(shù)據(jù)庫下創(chuàng)建表,所以我們這里還需要提前再做一步操作,這個(gè)操作就是use 數(shù)據(jù)庫的名字

表的增刪改查

1.表的創(chuàng)建

create table xxx (id int primary key auto_increment,name char not null,sex enmu('female','male'));

我們要記住創(chuàng)建表的語法結(jié)構(gòu)就行了。

create table 表名(

字段名1 類型[(寬度) 約束條件],

字段名2 類型[(寬度) 約束條件],

字段名3 類型[(寬度) 約束條件]

);

2.刪除表

drop table xxx

3.查看表

show tables

4.表的改

1. 修改表名

ALTER TABLE 表名

RENAME 新表名;

2. 增加字段

ALTER TABLE 表名

ADD 字段名 數(shù)據(jù)類型 [完整性約束條件…],

ADD 字段名 數(shù)據(jù)類型 [完整性約束條件…];

3. 刪除字段

ALTER TABLE 表名

DROP 字段名;

4. 修改字段

ALTER TABLE 表名

MODIFY 字段名 數(shù)據(jù)類型 [完整性約束條件…];

ALTER TABLE 表名

CHANGE 舊字段名 新字段名 舊數(shù)據(jù)類型 [完整性約束條件…];

ALTER TABLE 表名

CHANGE 舊字段名 新字段名 新數(shù)據(jù)類型 [完整性約束條件…];

5.修改字段排列順序/在增加的時(shí)候指定字段位置

ALTER TABLE 表名

ADD 字段名 數(shù)據(jù)類型 [完整性約束條件…] FIRST;

ALTER TABLE 表名

ADD 字段名 數(shù)據(jù)類型 [完整性約束條件…] AFTER 字段名;

ALTER TABLE 表名

CHANGE 字段名 舊字段名 新字段名 新數(shù)據(jù)類型 [完整性約束條件…] FIRST;

ALTER TABLE 表名

MODIFY 字段名 數(shù)據(jù)類型 [完整性約束條件…] AFTER 字段名;

5.查看表的結(jié)構(gòu)

desc 表名; desc staff_info;

show create table 表名 \G

show create table staff_info\G;

數(shù)據(jù)庫表之間的關(guān)系的操作

數(shù)據(jù)庫表之間的關(guān)系,我們要明白有三種的關(guān)系,然后我們要將這三種關(guān)系弄明白,手?jǐn)]一邊代碼。

一對(duì)多的關(guān)系:

create table press(

id int primary key auto_increment,

name varchar(20)

);

create table book(

id int primary key auto_increment,

name varchar(20),

press_id int not null,

foreign key(press_id) references press(id)

on delete cascade

on update cascade

);

一對(duì)一的關(guān)系

create table customer(

-> id int primary key auto_increment,

-> name varchar(20) not null,

-> qq varchar(10) not null,

-> phone char(16) not null

-> );

create table student(

-> id int primary key auto_increment,

-> class_name varchar(20) not null,

-> customer_id int unique, #該字段一定要是唯一的

-> foreign key(customer_id) references customer(id) #外鍵的字段一定要保證unique

-> on delete cascade

-> on update cascade

-> );

多對(duì)多

create table book(

id int primary key auto_increment,

name varchar(20),

press_id int not null,

foreign key(press_id) references press(id)

on delete cascade

on update cascade

);

create table author(

id int primary key auto_increment,

name varchar(20)

);

#這張表就存放作者表與書表的關(guān)系,即查詢二者的關(guān)系查這表就可以了

create table author2book(

id int not null unique auto_increment,

author_id int not null,

book_id int not null,

constraint fk_author foreign key(author_id) references author(id)

on delete cascade

on update cascade,

constraint fk_book foreign key(book_id) references book(id)

on delete cascade

on update cascade,

primary key(author_id,book_id)

);

我們這里再補(bǔ)充一些sql語句

登錄到數(shù)據(jù)庫服務(wù)器中:mysql -u 用戶名 -h ip地址 -p 密碼

select user();查看當(dāng)前的登錄的用戶。

select database();查詢當(dāng)前使用的數(shù)據(jù)庫。

set password = password('zhouqian');

create user 'zhouqian' @ '192.168.14.12' identified by '123';

grant 權(quán)限類型 on 數(shù)據(jù)庫(數(shù)據(jù)庫的名字).*(數(shù)據(jù)庫(數(shù)據(jù)庫的名字).表名) to 用戶('zhouqian' @ '192.168.14.%')

然后在最后我們這里要強(qiáng)調(diào)一點(diǎn)的就是,我們要對(duì)數(shù)據(jù)庫的數(shù)據(jù)類型和完整性約束有一個(gè)熟悉的了解,在這里我只是列舉出來,但是具體的學(xué)習(xí)大家自行去學(xué)習(xí)。

數(shù)據(jù)類型:int,float,date,datetime,char,varchar,enmu,set

約束條件:default,auto_increment,primary key,foreign key,not null,null,unique,

同時(shí)我在這里給大家推薦兩篇的博客:

總結(jié)

以上是生活随笔為你收集整理的mysql 原生 添加数据_手撸Mysql原生语句--增删改查的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。