数据库sql初识以及-增删改查

查看已有数据库show databases;

创建数据库:create database+数据库名字 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

删除数据库drop database +名字

进入数据库:use +数据库;

查看文件夹中所有数据表:show tables;

创建表:

sql 复制代码
create table 表名称(
	列名称 类型,
	列名称 类型,
	列名称 类型
)default charset=utf8;

实例:

sql 复制代码
create table tb1(
    id int,
    name varchar(16),
    age int
) default charset=utf8;
sql 复制代码
create table tb1(
    id int,
    name varchar(16) not null, -- 不允许为空
    age int null,             -- 允许为空
) default charset=utf8;
sql 复制代码
create table tb1(
    id int,
    name varchar(16) not null, -- 不允许为空
    age int default 3 -- 插入数据时,age列的值默认为3
) default charset=utf8;
sql 复制代码
create table tb1(
    id int primary key, -- 主键,不允许为空,不允许重复
    name varchar(16) ,
    age int          
) default charset=utf8;

主键一般用于表示当前行的数据的编号

sql 复制代码
create table tb1(
    id int auto_increament primary key, -- 主键,自增,内部维护
    name varchar(16) not null, -- 不允许为空 
    age int null,             -- 允许为空
) default charset=utf8;

一般的标准情况

sql 复制代码
create table tb1(
    id int not null auto_increment primary key,
    name varchar(16) ,
    age int 
) default charset=utf8;

查看表结构:desc tb1

常用数据类型:tinyint int bigint三个,数据范围不一样

表示小数float double decimal

准确的小数值,m是数字总个数(符号不算)d是小数后个数,m最大值为65,d最大值为30

sql 复制代码
create table tb3{
	id int not null primary key auto_increment,
	salary decimal(8,2)
}default charset=utf8;
  • char 定长字符串:char(11)固定用11个字符串进行存储
  • varchar变长字符串:varchar(11),真实数据有多长按多长存储
  • mediumtext
  • longtext
  • datetimeYYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59)
  • dateYYYY-MM-DD (1000-01-01/9999-12-31)

用户表:

sql 复制代码
create table tb7(
	id int not null primary key auto_increment,
    name varchar(64) not null,
    password char(64) not null,
    email varchar(64) not null,
    age tinyint,
    salary decimal(10,2),
    ctime datetime
)default charset=utf8;

插入数据

删除数据

sql 复制代码
delete from 表名
delete from 表名 where 条件
---
delete from tb2 where id=4 and name="侯卓林";
delete from tb2 where id>4;
delete from tb2 where id!=4
delete from tb2 where id in (1,5);

修改数据

sql 复制代码
update 表名 set 列=值
update 表名 set 列=值 where 条件
---
update tb2 set email="好好好" where id>5;
update tb2 set age=age+10 where id>5;

查询数据

sql 复制代码
select * from 表名称
select 列名称,列名称 from 表名称
select 列名称,列名称 from 表名称 where 条件
---
select * from tb7
select id,name from tb7 where id>10;
select id,name from tb7 where name="xx" and password="xx";

pycharm链接sql查询数据

py 复制代码
import pymysql
while True:
    user=input("请输入用户名:")
    if user.upper()=='0':
        break;
    pwd=input("请输入密码:")
    mobile=input("请输入手机号:")
    conn=pymysql.connect(host="127.0.0.1",port=3306,user='root',passwd="gushi713",charset='utf8',db='unicom')
    cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)
    #发送指令,这里不能用字符串格式化去做sql的拼接,会有sql注入的风险
    sql="insert into admin(username,password,mobile) values(%s,%s,%s)"
    cursor.execute(sql,[user,pwd,mobile])
    conn.commit()

    #关闭
    cursor.close()
    conn.close()

获取数据

python 复制代码
import pymysql


conn=pymysql.connect(host="127.0.0.1",port=3306,user='root',passwd="gushi713",charset='utf8',db='unicom')
cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)
#发送指令,这里不能用字符串格式化去做sql的拼接,会有sql注入的风险
sql="select * from admin"
cursor.execute(sql)
data_list=cursor.fetchall()
for data in data_list:
    print(data)

#关闭
cursor.close()
conn.close()

在执行增删改的时候一定记得commit,查询的时候不需要commit执行fetchall/fetchone

相关推荐
程序员清风6 小时前
数据处理脚本工程化:日志、配置、命令行与测试
服务器·开发语言·数据库
正在走向自律6 小时前
数据集成产品的技术底座:从日志解析原理到 KFS 同步链路搭建(含建表与增删改查完整代码)
数据库·oracle·信创·数据集成·国产数据库·kingbasees·全量+增量同步
程序员塔塔喵6 小时前
【MySQL篇|第七篇】MySQL事务
数据库·mysql·mysql事务
独隅7 小时前
Claude API 实战教程:从入门到完整项目
android·java·数据库·claude·claude code
_孤傲_7 小时前
快速掌握 MySQL 语法
数据库·后端·mysql
数字新视界9 小时前
一体化机柜在数据中心运维中的创新应用与优势分析
大数据·服务器·数据库·动环监控系统·动力与环境监控系统
SelectDB9 小时前
从 StarRocks 走向 Apache Doris:如何实现引擎平滑替换与架构升级?
大数据·数据库·数据分析
为思念酝酿的痛10 小时前
MySQL的操作
数据库·mysql
猫咪很可爱10 小时前
Oracle ORA-4031 错误分析与处理:SGA 内存不足导致 update/exp/expdp 失败
数据库·oracle
小豆庄11 小时前
视频素材处理时的隐私盲区:云处理与本地处理的边界在哪
数据库·图像处理·视频·敏捷流程