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

相关推荐
龙亘川1 小时前
【课程5.1】城管住建核心功能需求分析:市政设施、市容秩序等场景痛点拆解
数据库·oracle·智慧城市·城管住建
飞鸟真人1 小时前
Redis面试常见问题详解
数据库·redis·面试
cg50171 小时前
力扣数据库——组合两个表
sql·算法·leetcode
fanruitian2 小时前
Springboot项目父子工程
java·数据库·spring boot
super_lzb2 小时前
mybatis拦截器ParameterHandler详解
java·数据库·spring boot·spring·mybatis
CV工程师的自我修养2 小时前
数据库出现死锁了。还不知道什么原因引起的?快来看看吧!
数据库
码界奇点3 小时前
灵活性与高性能兼得KingbaseES 对 JSON 数据的全面支持深度解析
数据库·json·es
2501_941871453 小时前
面向微服务链路追踪与全局上下文管理的互联网系统可观测性设计与多语言工程实践分享
大数据·数据库·python
·云扬·3 小时前
MySQL单机多实例部署两种实用方法详解
数据库·mysql·adb
odoo中国3 小时前
Pgpool-II 在 PostgreSQL 中的用例场景与优势
数据库·postgresql·中间件·pgpool