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

相关推荐
IvorySQL19 分钟前
PG 日报|社区讨论重构 pg_hba 配置文件格式
数据库·人工智能·postgresql·重构·ivorysql
逝水无殇23 分钟前
C# 文件的输入与输出详解
开发语言·数据库·后端·c#
罗政2 小时前
PDF 批量合并工具:本地 AI 自动排序、识别正文日期与合同编号
数据库·pdf·php
SelectDB2 小时前
宽表元数据膨胀怎么解?Doris Segment V3 对比 Parquet、Lance
大数据·数据库·数据分析
Csvn2 小时前
📊 SQL 入门 Day 2:排序与去重 — 让数据有序呈现
sql
曹牧3 小时前
Oracle:快速定位最新数据
数据库·oracle
Zhu7583 小时前
在Docker环境部署ApacheGuacamole,对接PG数据库
数据库·docker·容器
Leighteen4 小时前
Redis与MySQL双写一致性:从问题根源到工程落地
数据库·redis·mysql
AI-好学者4 小时前
阶段二-Cypher查询语言详解
数据库·rag·graphrag