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

相关推荐
Hello.Reader23 分钟前
Flink SQL CREATE 语句从建表到 CTAS/RTAS,一次讲清
sql·flink·linq
amao998823 分钟前
数据库--dataset design
数据库
山沐与山1 小时前
【数据库】PostgreSQL架构与索引深度剖析
数据库·postgresql·架构
不穿格子的程序员2 小时前
Redis篇6——Redis深度剖析:从单机到集群,Redis高可用进化史
数据库·redis·集群·主从·高可用·哨兵
阿坤带你走近大数据2 小时前
什么是元数据管理?(附具体实施方案供参考)
数据库·金融
俊男无期2 小时前
超效率工作法
java·前端·数据库
2301_823438022 小时前
【无标题】解析《采用非对称自玩实现强健多机器人群集的深度强化学习方法》
数据库·人工智能·算法
中国胖子风清扬2 小时前
SpringAI和 Langchain4j等 AI 框架之间的差异和开发经验
java·数据库·人工智能·spring boot·spring cloud·ai·langchain
Elastic 中国社区官方博客2 小时前
Elasticsearch:你是说,用于混合搜索(hybrid search)
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
czlczl200209252 小时前
高并发下的 Token 存储策略: Redis 与 MySQL 的一致性
数据库·redis·mysql