数据库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

相关推荐
全栈老石13 小时前
拆解低代码引擎核心:元数据驱动的"万能表"架构
数据库·低代码
倔强的石头_1 天前
kingbase备份与恢复实战(二)—— sys_dump库级逻辑备份与恢复(Windows详细步骤)
数据库
jiayou643 天前
KingbaseES 实战:深度解析数据库对象访问权限管理
数据库
李广坤3 天前
MySQL 大表字段变更实践(改名 + 改类型 + 改长度)
数据库
爱可生开源社区4 天前
2026 年,优秀的 DBA 需要具备哪些素质?
数据库·人工智能·dba
随逸1775 天前
《从零搭建NestJS项目》
数据库·typescript
加号35 天前
windows系统下mysql多源数据库同步部署
数据库·windows·mysql
シ風箏5 天前
MySQL【部署 04】Docker部署 MySQL8.0.32 版本(网盘镜像及启动命令分享)
数据库·mysql·docker
李慕婉学姐5 天前
Springboot智慧社区系统设计与开发6n99s526(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
百锦再5 天前
Django实现接口token检测的实现方案
数据库·python·django·sqlite·flask·fastapi·pip