加班整理出来的MySQL数据库基本操作送给大家,非常详细!
哈喽兄弟们,中秋闲着没事,整理了一些数据库的基本操作,分享给大家,希望对大家有所帮助~https://img-blog.csdnimg.cn/944e9e7a210d4490a1d9e634908a4edc.png
一、SQL语句 (mysql 数据库中的语言)
show databases;查看数据库
use "database_ name" ;进入数据库
show tables; 查看当前数据库中有哪些表
select * from "table_ name";查询数据表中的所有内容
describe "table_ name"; 查看表结构
desc "table_ name";
https://img-blog.csdnimg.cn/f3ac46ffe49340b5bf354ea89d568b49.png
https://img-blog.csdnimg.cn/91cd8581769b4535b6a92adba5a0861f.png
https://img-blog.csdnimg.cn/de3ada39ef954485b4004ac2846e19ec.png
https://img-blog.csdnimg.cn/c2d456dc6da548f3b6e715cae040fe73.png
https://img-blog.csdnimg.cn/2e4c77f6c145459c923c3e987170fee7.png
https://img-blog.csdnimg.cn/8ab0fcb212484f0f80022931d4e3d030.png
https://img-blog.csdnimg.cn/d1fe5498f76948e1b19c649254d60195.png
类比excel表格
https://img-blog.csdnimg.cn/25ba5f0b4c0c43f094f37748a0c86db5.png
https://img-blog.csdnimg.cn/3b96f1273e4c43269a613a7a7b1b81b9.png
https://img-blog.csdnimg.cn/7058fb80a6434a6ab8214bc1d6476120.png
https://img-blog.csdnimg.cn/d11a0cae9bbc4b98aa0032fe5b7b0c70.png
类比excel表格
https://img-blog.csdnimg.cn/31832929136b42e5a158dc9c229a11d6.png
https://img-blog.csdnimg.cn/2d48048108d14ea9a79157691cc5db47.png
简写
filed 字段名
https://img-blog.csdnimg.cn/fa344fd5a5dd4831b9624da6c3d50193.png
二、DDL
1.DDL语句
用于创建数据库对象(库、表、索引等)
(1)创建新的数据库
create database 数据库名;
# Python学习交流群 279199867
(2)创建新的表
create table 表名(字段1 数据类型,字段2 数据类型[, …] [, primary key (主键名)]);
主键一般选择能代表唯一性的字段不允许取空值(NULL) ,一个表只能有一个主键。
create database 数据库名
use 数据库名
create table 表名 (id int not null, name char(10) not null, score decimal (5,2) ,passwd char (48) defalt' ',primarykey (id)) ;
desc 表名
not null 不允许为空值
default ' ' 默认值为空
primary key : 主键一般选择没有重复并且不为空值的字段
例子
create table 表名 (id int(10) not null primary key, name varchar(40) ,age int(3));
create table food (id int(3) , name varchar (40) ,money decimal (3,1) ,primary key (id));
https://img-blog.csdnimg.cn/dc4c65e8c5164d31aaaefac51682890a.png
https://img-blog.csdnimg.cn/4327c3259cd24d0e94d58b9a375f9140.png
https://img-blog.csdnimg.cn/0bde7fa369e54c4eb4437bd6156763e9.png
https://img-blog.csdnimg.cn/b66f00ae65374d8d8e145729aec7419a.png
https://img-blog.csdnimg.cn/0f5db16074604dc7bf61c013644f4831.png
2.删除数据库和表
删除指定的数据表
drop 删除表内容(数据)和表结构
use数据库名
drop table 表名
drop table [数据库名.] 表名;
如不用use进入库中,则需加上数据库名
删除指定的数据库
drop database 数据库名;
https://img-blog.csdnimg.cn/884d083406eb420087be985c16d2f36c.png
https://img-blog.csdnimg.cn/b1d85973d4834cfcb9a231bad7bc60d2.png
https://img-blog.csdnimg.cn/e3db058df9314ff5939a0090310c19b6.png
https://img-blog.csdnimg.cn/884ad7c3497b41c39adb32987be79e8b.png
https://img-blog.csdnimg.cn/ee26d5cad08a49fcb6fbf94fd9d89edd.png
https://img-blog.csdnimg.cn/b4d08721ef1144f78c9c4a8b6cf937d6.png
https://img-blog.csdnimg.cn/b03dfab9d3484ecf97d7cf588d08ffad.png
https://img-blog.csdnimg.cn/00ddee2315c449b28989fcb55f2b9f79.png
https://img-blog.csdnimg.cn/6fcf76110c684641954d9533bc463adb.png
三、DML
管理表中的数据记录
insert: 插入新数据
update: 更新原有数据
delete: 删除不需要的数据
1.insert插入新数据
格式:
insert into 表名(字段1,字段2[,...]) values (字段1的值,字段2的值,...);
例子:
insert into 表名 (id,name,score,passwd) values (1,'自定义',70.5,passwd('123456')) ;
passwd(‘123456’) :查询数据记录时,密码字串以加密形式显示:若不使用passwd(), 查询时以明文显示。
密码复杂性验证
insert into 表名 values(2,'自定义',90.5, 654321) ;
select * from 表名 ; 查询表的数据记录
insert插入表数据
在此之前需要进行查看desc table_ name; 来查看表结构(有哪些字段,有无主键,主键是哪个字段,type,是否允许为空,是否有默认值)
使用insert into table_ name进行插入,是根据查看到的表结构来判断,可以怎么写
https://img-blog.csdnimg.cn/eeb25a1a35d645208175fb212e1dcf4a.png
https://img-blog.csdnimg.cn/b7b37c48ccef45368f849b38512c74e3.png
https://img-blog.csdnimg.cn/5708ae3756d4412f9cddc5b224078f89.png
2.update更新原有数据
修改、更新数据表中的数据记录
格式:
update 表名 set 字段名1=字段值1[,字段名2=字段值2] ;
例子:
update 表名 set passwd=PASSWORD('') where name='自定义';
update 表名 set name= '自定义' , passwd='' where id=3;
https://img-blog.csdnimg.cn/e7153b4cef1048699a126aa115bdc4df.png
https://img-blog.csdnimg.cn/bd9744859f674c30990999c144c9d292.png
https://img-blog.csdnimg.cn/0e7a60c8a6e64b98a52d905bc50d0f5f.png
3.delete: 删除不需要的数据(表内容)
在数据表中删除指定的数据记录(行)
格式:.
delete from 表名 ;
例子:
delete from 表名 where id=4;
https://img-blog.csdnimg.cn/8016644fc36b4bbe8cefab8bb2599067.png
四、DQL查询数据记录
select
格式:
seleect 字段名1,字段名2[,...] from 表名;
例子:
seleect * from 表名;
seleect id, name from 表名;
seleect id, name, score from 表名 where id=2;
select name from 表名\G 以列表方式竖向显示
select * from info limit 2; 只显示头3行
select * from info limit 2,3; 显示第3行后的前3行
https://img-blog.csdnimg.cn/6cafe29ac0a74d02939bdf01cf2a9f67.png
https://img-blog.csdnimg.cn/2c78e261bcc54b229b9a1b2524704a3a.png
https://img-blog.csdnimg.cn/69b4db8953f84b64ae296485f895c954.png
https://img-blog.csdnimg.cn/80bbe0c242f14e78b09e35328819f872.png
https://img-blog.csdnimg.cn/76f36ed6d95a4628a0626c581a3d12a2.png
类比excel表格
https://img-blog.csdnimg.cn/a7652838182d497d8a6afa6243ae23ff.png
四、DCL
1.alter 修改表名和表结构(表结构)
alter table 旧表名 rename 新表名;
扩展表结构(增加字段)
alter table 表名add address varchar(50) default '地址不详' ;
default ' 地址不详':表示此字段设置默认值为地址不详,可与not null配合使用
alter table 表名 add address varchar(50) not null default '地址不详' ;
修改字段(列)名,添加唯一键(唯一性约束)
alter table 表名 change 旧列名 新列名 数据类型 ;
unique key:唯一键(特性:唯一, 但可以为空,空值只允许出现一次)
primary key (主键) :唯一且非空
alter table 表名 change name user_ name varchar(10) unique key;
change可修改字段名、数据类型、约束等所有项。
删除字段
格式:
alter table 表名 drop 字段名;
兄弟们,今天的分享就到这里结束了,下次见~
如果本文对你有所帮助的话,记得点赞收藏呀~
最后推荐一套Python教程:Python实战100例
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
页:
[1]