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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

MySQL基本指令汇总

發布時間:2023/12/20 数据库 13 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MySQL基本指令汇总 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

創建數據庫:

create database? 數據庫名字;

?

刪除數據庫:

drop database 數據庫名字;

?

查看數據庫:

show databases;

?

切換數據庫:

use databasename;

?

select database();

?

Create table 表名(列名? 數據類型 [約束],列名? 數據類型 [約束],列名? 數據類型 [約束]);

?

刪除表:

drop table 表名;

?

查看表結構:

desc 表名;

?

查看建表的語句:

show create table表名;

insert? update? delete

show variables like 'char%';

show variables like 'collation%';

sudo vim /etc/mysql/my.cnf

添加以下內容

[mysqld]

collation-server = utf8_unicode_ci

init-connect='SET NAMES utf8'

character-set-server = utf8

重啟:

service mysql restart

?

alter? 修改表結構:

添加字段:

alter table 表名 add? [column]? 字段名? 數據類型?? [約束];

?

刪除字段:

alter table 表名 drop? [column]? 字段名;

?

修改字段的名稱和類型

alter table 表名 change? 舊字段名?? 新字段名?? 數據類型? [約束];

?

修改字段的類型

alter table 表名? modify? 字段名? 新的數據類型? [約束];

?

?

約束:

主鍵約束: 主鍵? =? 唯一? + not null

?

primary key

自增: auto_increment

create table food(id int primary key auto_increment,name varchar(20),price float)

?

非空約束:

not null

create table qq(id int primary key auto_increment,nick_name varchar(16) not null,password varchar(64) not null,qq_no varchar(10) not null);

?

默認值約束:

default

create table? book(id int primary key auto_increment,bname varchar(30) not null, publisher varchar(50) default '默認值');

?

insert into book(bname,publisher) values ('daomubijia',null)

?

select * from book;

?

insert into book(bname,publisher) values('daomubiji',default);

?

?

唯一約束:

unique?? 保證數據的唯一性

但是允許null

create table? user(id int primary key auto_increment,username varchar(16) unique,password varchar(64) not null,phone char(11) unique not null);

?

?

檢查約束:mysql? ---- 不支持 check

create table user(id int primary key auto_increment,username varchar(16) unique,password varchar(64) not null,phone char(11) unique,gender? enum('',''));

?

?

外鍵約束:

foreign key?

ALTER TABLE score1 ADD CONSTRAINT fk_sid FOREIGN KEY(sid) REFERENCES stu(sid)

?

一張表可以有多個外鍵,只能有一個主鍵(idusername+phone

?

username?? phone

admin?????????? 18900001111

admin?????????? 18900001112

?

?

?

添加數據:

insert into 表名(id,name,age)? values(1,值2,值3)

id

name

age

101

aa

19

?

?

?

修改數據:

update 表名? set? 字段名=新值;?? ---- 更新該字段下的所有的值

?

update 表名? set? 字段名=新值 where 條件

?

案例:

?

update student set age =18;

?

update student set age=19 where id =3;

?

update student set age =20 where id =1 or? id=5;

?

update student set age =age+1? where? id>=2;

?

update student set age=age+1 where id in (2,4,5,8,10);

?

--? 1-100?? 50~70

?

update student set age =age+1? where? id>=50 and id<=70;

?

update student set age =age+1? where id between 50 and 70;

?

?

update student set age=age+1,name='laowang' where id =4;

?

?

刪除數據:

?

delete from 表名 where 條件】

?

delete from student where id=4;

總結

以上是生活随笔為你收集整理的MySQL基本指令汇总的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。