SQL语句:
创建数据库:create database atm;
删除数据库:Drop database if exists atm;
进入数据库:use atm;
创建表:create table account
As(
Coad varchar(32) NOTNULL,
Pass int(6) NOTNULL,
Money double(15,2) NOTNULL,
PRIMARY KEY (coad)
);
删除表:drop table account;
Drop table if exists account;
删除表中的所有数据,但仍保持表的完整性:truncate user,也可以用delete from user,相对来说,truncate user的效率更高
插入数据:
Insert into account values(‘1111’,111,111.00);
Insert into account (coad,pass) values (‘1111’,111);(选择其中的某些字段插入数据,注意,其他的属性要允许为空)
将表E1中的数据复制到表E:insert into E select * from E;
将表E2中(E_ID,E_NAME,E_DATE,E_SEX)中的记录追加到表E中,insert into (E_ID,E_NAME,E_DATE,E_SEX ) select E_ID,E_NAME,E_DATE,E_SEX from E2;
一个数据库中的表中的数据复制到另一个数据库中的一个表,使用方法如下:
insert into 数据库A.dbo.table1(col1,col2,col3...) select col1,col2,col3... from 数据库B.dbo.table2
更改数据:
Update account set pass =1122 where coad=’1111’;(只对其中一组数据的pass进行修改)
Update account set pass=1112;(对某一字段进行修改)
条件查询:select * from account where coad=’1111’;(查询coad=’1111’这组数据各个属性的数据)
select * from account where coad=’1111’ order by id desc(asc);(将查到的coad=’1111’这组数据按id排序)
Select pass from account where coad=’1111’;
Select *from account where coad like ‘[11]%’ ([]指定值的范围,但是在sql语句中不用写[])
select * from ft_order where f_id in (select f_id from ft_food where f_title = "白灼虾")(子查询, 内层select返回值是多个;如果内层select返回值时可以用”=”代替in)
select * from 数据表 where 字段名 between 值1 and 值2"(限制查询)
select * from 数据表 order by 字段名 limit a,b(a,b分别是查询开始和结束的位置)
select all/distinct <目标表达式>[,<目标表达式>]… from <表名或视图名>[,表名或视图名] [ where <条件表达式>] [group by <列名1> [having <条件表达式>]] [order by <列名2> [ASE/DESC]…];distinct选项保证查询的结果集中不存在重复元组。
select * from klass ,student where klass.class_id=student.class_id where COUNT(stu_id)>3;
查询时间先后时,可以用>或<
查询条件:
确定范围:between and,not between and
确定集合:in,not in
字符匹配:like,not like
空值:is null ,is not null;=’’;
多重条件查询:and,or,not
查询前10%:
select top 10 percent stu_id,grade from studentgrade
where course_id='0511'
order by grade desc
聚集函数:
Count(*):统计元组个数
Count([distinct/all]<列名>):统计一列中值的个数,如select count(distinct user_id)from user;
Sum([DISTINCT/ALL]<列名>):计算一列值的总和(必须是数值型)
Avg([distinct/all]<列名>):计算一列值的平均值
Max([distinct /all]<列名>):求一列值中的最大值
Min([distinct /all]<列名>):求一列值中的最小值
连接查询(join):Join可以看成是把几张表的相关内容进行横向组合。
联合查询:联合是指把几个相关的查询结果纵向组合,每个查询结果的字段数量、和种类都是相同的。联合在数据库查询中是经常使用的。当然,联合查询也可以实现连接查询的效果。
查询所有数据:
Select * from account;
Select money from account;
删除数据:
Delete from account;
Delete from account where coad=’1111’;
Delete t1,t2 from t1,t2 where t1.id=t2.id and t1.id=’1111’;
标准的SQL语言提供以下常用的5种组函数
MIN( ) 是最小值函数;
MAX() 是最大值函数;
COUNT()是计数函数;
SUM() 是求和函数;
AVG() 是求均值函数;
双引号的作用是:如果创建对象的时候,对象名、字段名加双引号,则表示sql将严格区分大小写,否则都默认大写。
而单引号则表示:这个加了单引号的字段是一个字类似字符串,并不区分大小写,单引号用于标识字符与数字的区别
授权:grant <权限>[,<权限>]… on <对象类型><对象名>] to <用户> [,<用户>] …[with grant option] ;如:grant insert on Table Student to userA with grant option;
收回授权:revoke <权限>[,<>]… [on <对象类型><对象名>] from <用户> [,<用户>]…
建立数据库联接:
1. 加载驱动:Class.forName(“com.mysql.jdbc.Driver”);
2. 建立连接: java.sql.Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/atm","root","root");//数据库名,用户名,密码
3. 创建传送对象:Statement stmt=conn.Statement();
4. 执行语句:
查询:ResultSet rs=stmt.executeQuery(“insert * from account ## where ##=##”);
修改:stmt.executUpdate(“update account set ##=## where ##=##”);
插入:stmt.executeUpdate(“insert into account values(##,##,##)”);
注意:在执行语句中如果用到符号,要用”+符号+”(同时,表示字符串的加上字符串本身的’’,表示数字的直接用”+符号+”)
executeUpdate没有返回值