1. 首页 > 智能数码 >

增删改查sql语句 增删改查sql语句例子

sql数据库常用方法怎么增删改查

结构化查询语言(StructuredQueryLanguage)简称SQL,是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统;同时也是数据库脚本文件的扩展名。今天就给大家介绍数据库的基本SQL作增删改查!!!

增删改查sql语句 增删改查sql语句例子增删改查sql语句 增删改查sql语句例子


材料/工具

电脑SQLServer

为表添加主键

altertable

主键添加前:

主键添加后:

插入数据

insertinto

查询

查询全部记录:

selectfrom

条件查询(查询全部字段用,查询指定字段也可以):

select

带有Sql函数的查询:

selectcount()from

模糊查询(like语法):

select

删除(Delete)

deletefrom

更新(update)

update

为表添加一列

注意:列增加后不可删除。DB2中的列加上后数据类型也不能改变,能改变的是增加varchar的长度。

altertable

sql增删改查语句怎么写

sql中的增删改查语句是用来对数据库中数据进行作的,所以我们这篇文章就来看一下SQL增删查改语句的具体写法。

一、SQL语句之增

insert into 表的名字 (字段名)values(值);

向student表中插入一个学生的数据

insert into student (num,name,sex,age)

values(140010,张三,男,23)二、SQL语句之删

删除student表中num=140011的这条数据。

delete from student where num=140011;三、SQL语句之改

我们可以将num为140010的age值更改为21。

update student set age =21 where ID=140010;四、SQL语句之查

查询语句非常的重要的,所以需要详细来说一下。

1、查询student表中所有数据

select from student;2、查询student表中所有的name和sex

select name,sex from student;3、查询num为140010这一行的数据

select from where id =140010;

如何在python使用mysql的增删改查

非原创

1、建立一张表

#!/usr/bin/python3 import pymysql

# 打开数据库连接db=pymysql.connect(‘localhost‘,‘wjh‘,‘123456‘,‘test‘)

# 使用 cursor() 方法创建一个游标对象 cursorcursor = db.cursor()

# 使用 execute() 方法执行 SQL,如果表存在则删除#cursor.execute("DROP TABLE IF EXISTS EMPLOYEE") # 使用预处理语句创建表sql = """CREATE TABLE EMPLOYEE (FIRST_NAME CHAR(20) NOT NULL,LAST_NAME CHAR(20),AGE INT, SEX CHAR(1),INCOME FLOAT )"""

# SQL 删除语句sql = "DELETE FROM EMPLOYEE WHERE AGE > ‘%d‘" % (20)

# SQL 插入语句sql = """INSERT INTO EMPLOYEE(FIRST_NAME, LAST_NAME, AGE, SEX, INCOME) VALUES (‘Mac‘, ‘Mohan‘, 20, ‘M‘, 2000)"""

===========================================

下面是查询用法:

import pymysql # 打开数据库连接db = pymysql.connect("localhost","wjh","123456","test" ) # 使用cursor()方法获取作游标 cursor = db.cursor() # SQL 查询语句sql = "SELECT FROM EMPLOYEE \ WHERE INCOME > ‘%d‘" % (1000)try: # 执行SQL语句 cursor.execute(sql) # 获取所有记录列表 results = cursor.fetchall() for row in results: fname = row[0] lname = row[1] age = row[2] sex = row[3] income = row[4] # 打印结果 print ("fname=%s,lname=%s,age=%d,sex=%s,income=%d" % \ (fname, lname, age, sex, income ))except: print ("Error: unable to fetch data")# 关闭数据库连接db.close()

======================

又一种查询

import pymysql#打开数据库db=pymysql.connect(‘localhost‘,‘wjh‘,‘123456‘,‘test‘)# 使用 cursor() 方法创建一个游标对象 cursorcursor=db.cursor()# 使用 execute() 方法执行 SQL 查询cursor.execute("select from user")# 使用 fetchone() 方法获取单条数据.data=cursor.fetchone()print(data)

# 关闭数据库连接db.close()

==========================

更新:

#!/usr/bin/python3 import pymysql # 打开数据库连接db = pymysql.connect("localhost","wjh","123456","test" ) # 使用cursor()方法获取作游标 cursor = db.cursor() # SQL 更新语句sql = "UPDATE EMPLOYEE SET AGE = AGE + 1\ WHERE SEX = ‘%c‘" % (‘M‘)try: # 执行SQL语句 cursor.execute(sql) # 提交到数据库执行 # 发生错误时回滚 db.rollback() # 关闭数据库连接db.close()

cursor.execute(sql) # 关闭数据库连接db.close()

如何在python使用mysql的增删改查

标签:fetchallpython3exetryhar用法selectpymysqlerr

用SQL语句随便写一条数据库增删改查语句

一、增:有2种方法

1.使用insert插入单行数据:

语法:insert [into] <表名> [列名] values <列值>

例:insert into Strdents (姓名,性别,出生日期) values ('王伟华','男','1983/6/15')

注意:如果省略表名,将依次插入所有列

2.使用insert,select语句将现有表中的 数据添加到已有的新表中

语法:insert into <已有的新表> <列名> select <原表列名> from <原表名>

例:insert into addressList ('姓名','地址','电子邮件')select name,address,email

from Strdents

注意:查询得到的数据个数、顺序、数据类型等,必须与插入的项保持一致

二、删:有2中方法

1.使用delete删除数据某些数据

语法:delete from <表名> [where <删除条件>]

例:delete from a where name='王伟华'(删除表a中列值为王伟华的行)

注意:删除整行不是删除单个字段,所以在delete后面不能出现字段名

2.使用truncate table 删除整个表的数据

语法:truncate table <表名>

例:truncate table addressList

注意:删除表的所有行,但表的结构、列、约束、索引等不会被删除;不能

用于有外建约束引用的表

三、改使用update更新修改数据

语法:update <表名> set <列名=更新值> [where <更新条件>]

例:update addressList set 年龄=18 where 姓名='王伟华'

注意:set后面可以紧随多个数据列的更新值(非数字要引号);where子句是可选的(非数字要引号),用来限制条件,如果不选则整个表的所有行都被更新

四、查

语法:select <列名> from <表名> [where <查询条件表达试>] [order by <排序的列

名>[asc或desc]]

1).查询所有数据行和列

例:select from a

说明:查询a表中所有行和

2).查询部分行列--条件查询

例:select i,j,k from a where f=5

说明:查询表a中f=5的所有行,并显示i,j,k3列

3).在查询中使用AS更改列名

例:select name as 姓名from a where gender='男'

说明:查询a表中性别为男的所有行,显示name列,并将name列改名为(姓名)显示

4).查询空行

例:select name from a where email is null

说明:查询表a中email为空的所有行,并显示name列;SQL语句中用is null或者is not null

来判断是否为空行

5).在查询中使用常量

例:select name '北京' as 地址 froma

说明:查询表a,显示name列,并添加地址列,其列值都为'北京'

6).查询返回限制行数(关键字:top )

例1:select top 6 name from a

说明:查询表a,显示列name的前6行,top为关键字(oracle 中没有top关键字

用rownum替代)

select from a where rownum<6

7).查询排序(关键字:order by , asc , desc)

例:select name

from a

where grade>=60

order by desc

说明:查询表中成绩大于等于60的所有行,并按降序显示name列;默认为ASC升序

sql语句的增删改查

1、数据库增加数据:

1)插入单行

insert [into] <表名> (列名) values (列值)

例:insert into t_table (name,sex,birthday) values ('开心朋朋','男','1980/6/15')

2)将现有表数据添加到一个已有表 insert into <已有的新表> (列名) select <原表列名> from <原表名>

例:insert into t_table ('姓名','地址','电子邮件')

select name,address,email from t_table

3)直接拿现有表数据创建一个新表并填充 select <新建表列名> into <新建表名> from <源表名>例:select name,address,email into t_table from strde

2、数据库删除数据:

1)删除<满足条件的>行

例:delete from t_table where name='开心朋朋'(删除表t_table中列值为开心朋朋的行)

2)删除整个表 truncate table <表名>

truncate table tongxunlu

注意:删除表的所有行,但表的结构、列、约束、索引等不会被删除;不能用语有外建约束引用的表

3、数据库修改数据 update <表名> set <列名=更新值> [where <更新条件>]

例:update t_table set age=18 where name='蓝色小名'

4、数据库查询数据:

1)(条件)查询

2)查询所有数据行和列。例:select from a

说明:查询a表中所有行和列

3)使用like进行模糊查询

注意:like运算副只用于字符串,所以仅与char和varchar数据类型联合使用

例:select from a where name like '赵%'

说明:查询显示表a中,name字段个字为赵的记录

4)使用between在某个范围内进行查询

例:select from a where nianling between 18 and 20

说明:查询显示表a中nianling在18到20之间的记录

5)使用in在列举值内进行查询

例:select name from a where address in ('北京','上海','唐山')

说明:查询表a中address值为北京或者上海或者唐山的记录,显示name字段

扩展资料:

插入之前需要创建数据表,创建方式如下:

CREATE TABLE 表名称

例如:--流程步骤定义表

create table T_flow_step_def(

Step_no int not null, --流程步骤ID

Step_name varchar(30) not null, --流程步骤名称

Step_des varchar(64) not null, --流程步骤描述

Limit_time int not null, --时限

URL varchar(64) not null, --二级菜单链接

Remark varchar(256) not null,

)参考资料:

4、说明:创建新表

create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)

根据已有的表创建新表:

A:create table tab_new like tab_old (使用旧表创建新表)

B:create table tab_new as select col1,col2… from tab_old definition only

5、说明:删除新表

drop table tabname

6、说明:增加一个列

Alter table tabname add column col type

注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,能改变的是增加varchar类型的长度。

7、说明:添加主键: Alter table tabname add primary key(col)

说明:删除主键: Alter table tabname drop primary key(col)

8、说明:创建索引:create [unique] index idxname on tabname(col….)

删除索引:drop index idxname on tabname

注:索引是不可更改的,想更改必须删除重新建。

9、说明:创建视图:create view viewname as select statement

删除视图:drop view viewname

10、说明:几个简单的基本的sql语句

选择:select from table1 where 范围

插入:insert into table1(field1,field2) values(value1,value2)

删除:delete from table1 where 范围

更新:update table1 set field1=value1 where 范围

查找:select from table1 where field1 like ’%value1%’ (所有包含‘value1’这个模式的字符串)---like的语法很精妙,查资料!

排序:select from table1 order by field1,field2 [desc]

总数:select count as totalcount from table1

求和:select sum(field1) as sumvalue from table1

平均:select g(field1) as gvalue from table1

:select max(field1) as maxvalue from table1

小:select min(field1) as minvalue from table1[separator]

添加:

insert

into

table(a,b)

vlaue(111,222);

删除:

delete

from

table

where

id=1;

改:

update

table

set

a=

333

where

id=2;

查:

select

from

table

where

id=4;

用SQL语句随便写一条数据库增删改查语句?

表名: person\x0d\x0a字段: id, name, age\x0d\x0a 1 张三 20\x0d\x0a 2 李四 22\x0d\x0a 3 王五 23\x0d\x0a\x0d\x0a查询: select id,name,age from person;\x0d\x0a删除: delete from person where id=1 (删除ID=1的那条数据,)\x0d\x0a delete from person (删除person表中的所有数据);\x0d\x0a修改: update person set name="刘德华" where id=2; (就会李四的名字改成刘德华);\x0d\x0a增加: insert into person values(4,'赵六',24);

增删改查sql语句

1、数据库增加数据:

1)插入单行

insert [into] <表名> (列名) values (列值)

例:insert into t_table (name,sex,birthday) values ('开心朋朋','男','1980/6/15')

2)将现有表数据添加到一个已有表 insert into <已有的新表> (列名) select <原表列名> from <原表名>

例:insert into t_table ('姓名','地址','电子邮件')

select name,address,email from t_table

3)直接拿现有表数据创建一个新表并填充 select <新建表列名> into <新建表名> from <源表名>例:select name,address,email into t_table from strde

2、数据库删除数据:

1)删除<满足条件的>行

例:delete from t_table where name='开心朋朋'(删除表t_table中列值为开心朋朋的行)

2)删除整个表 truncate table <表名>

truncate table tongxunlu

注意:删除表的所有行,但表的结构、列、约束、索引等不会被删除;不能用语有外建约束引用的表

3、数据库修改数据 update <表名> set <列名=更新值> [where <更新条件>]

例:update t_table set age=18 where name='蓝色小名'

4、数据库查询数据:

1)(条件)查询

2)查询所有数据行和列。例:select from a

说明:查询a表中所有行和列

3)使用like进行模糊查询

注意:like运算副只用于字符串,所以仅与char和varchar数据类型联合使用

例:select from a where name like '赵%'

说明:查询显示表a中,name字段个字为赵的记录

4)使用between在某个范围内进行查询

例:select from a where nianling between 18 and 20

说明:查询显示表a中nianling在18到20之间的记录

5)使用in在列举值内进行查询

例:select name from a where address in ('北京','上海','唐山')

说明:查询表a中address值为北京或者上海或者唐山的记录,显示name字段

扩展资料:

插入之前需要创建数据表,创建方式如下:

CREATE TABLE 表名称

例如:--流程步骤定义表

create table T_flow_step_def(

Step_no int not null, --流程步骤ID

Step_name varchar(30) not null, --流程步骤名称

Step_des varchar(64) not null, --流程步骤描述

Limit_time int not null, --时限

URL varchar(64) not null, --二级菜单链接

Remark varchar(256) not null,

)参考资料:

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至836084111@qq.com 举报,一经查实,本站将立刻删除。

联系我们

工作日:9:30-18:30,节假日休息