mysql增删改查sql语句

未经允许,禁止转载!!!未经允许,禁止转载!!!

创建表   create table 表名
删除表    drop table 表名
修改表名   rename table 旧表名 to 新表名字
创建数据库   create database 数据库名
删除数据库   drop database 数据库名
修改数据库名   rename database 旧名字 to 新名字

复制表    create table 表名字1 as select*frome table 表名字2
将符合条件的保存到另外一张表  select*into 表名字2 from 表名字1 where 工资>10000
合并表  select*from 表名字1 union select*from 表名字2
将两个表放在一起分开显示  select*from 表名字1 interset select*from 表名字2

查询表中所有字段/列   select*from 表名字
查询多个字段/列   select name,age,address from 表名字
查询表前20行   select top(20) name,age,address from 表名字
按顺序查找   select*from 表名字 order by 年龄 asc
按顺序查找   select*from 表名字 order by 年龄 asc,salary asc

添加一个字段/列    alter table 表名字 add age int
添加多个字段/列  alter table 表名字 add age int,add 邮箱地址 varchar(11)
删除一个字段/列     alter table 表名字 drop column 邮箱地址
删除多个字段/列      alter table 表名字 drop column 邮箱地址,name,age
修改字段属性  alter table 表名字 alter column 邮箱地址 char
给某一字段统一输入值   update 表名字 set salary=3000
添加字段/列并设置默认值   alter table 表名字 add 邮箱地址 varchar(60) default '162@.com'

删除数据库数据文件  alter database 数据库名字 remove file 文件名字
删除数据库日志文件  alter database 数据库名字 remove file 日志文件名字

查询表的行数  select count(*) from 表名字
为表增加一行值   insert into 表名字 (id, name, age, salary) values (001, MR James,40,10000)

批量插入数据    insert into tablename  (name,id…)  values (‘user1’,1....), (‘user2’,2....), (‘user3’,3....);
更新表中一行数据    update 表名字 set id=001,name=Tom,age=30,salary=5000 where id = 001


查询符合条件 select*from 表名字 where salary<=5000
查询符合条件 select*from 表名字 where salary>10000 or salary<1000
查询符合条件 select*from 表名字 where salary>10000 or address='上海'
查询符合条件 select*from 表名字 where address !='上海'
查询符合条件 select*from 表名字 where not address='上海'
查询符合条件 select*from 表名字 where age>40 and salary<10000
查询符合条件 select*from 表名字 where(age>40 or id>020) and salary<10000
查询符合条件 select*from 表名字 where address in ('北京','上海','广州')
查询符合条件 select*from 表名字 where address not in ('北京','上海','广州')
查询符合条件 select*from 表名字 where salary in(select salary from 表名字 where name='James')
查询符合条件 select*from 表名字 where name in(selcet distinct name from 另一个表)order by age asc
查询符合条件 select*from 表名字 where name in(selcet distinct name from 另一个表)order by age desc
查询符合条件 select*from 表名字 where exists(select*from 表名字 where 毕业院校 = '北京大学')
查询符合条件 select*from 表名字 where salary > any(select*from 表名字 where age=30)
查询符合条件 select*from 表名字 where salary > all(select*from 表名字 where age=30)


select avg(工资) as 平均工资 from 表名字
select max(工资) as 平均工资 from 表名字
select min(工资) as 平均工资 from 表名字


直接创建索引 create index 索引名字 on 表名字(字段名/列名)
修改表结构的方式添加索引alter table 表名字 add index 索引名字(字段名/列名)
直接创建唯一索引 create union index 索引名字 on 表名字(字段名/列名)
修改表结构的方式添加唯一索引alter table 表名字 add union index 索引名字(字段名/列名)
删除索引 drop index 索引名字 on 表名字

Mysql储存过程简介:
储存过程是一个可编程的函数,它在数据库中创建并保存。它可以有SQL语句和一些特殊的控制结构组成。

当希望在不同的应用程序或平台上执行相同的函数,或者封装特定功能时,存储过程是非常有用的。

数据库中的存储过程可以看做是对编程中面向对象方法的模拟。它允许控制数据的访问方式。存储过程通常有以下优点:

存储过程能实现较快的执行速度。
编译优化,快!

存储过程允许标准组件是编程。
封装与抽象,简单调用

有很强的灵活性,可以完成复杂的判断和较复杂的运算
功能强大,逻辑强大

存储过程可被作为一种安全机制来充分利用。
限制与安全

存储过程能过减少网络流量。
减少网络流量(封装的好)

create procedure  存储过程名字

as

begin

print'hello world!'

end

修改存储过程直接将上面的create换成alter    调用存储过程exec/execute 存储过程名字

原文地址:https://www.cnblogs.com/111testing/p/6623059.html