mysql關鍵字使用:
放棄正在輸入的命令:\c
退出sql程序:\q
查看mysql服務器狀態信息:\s
域完整性:主鍵primary key; 外鍵foreign key;
建立一個數據庫:create database 庫名;
創建表及列名:create table 表名(
列名1 數據類型 約束,
列名2 數據類型 約束,
列名3 數據類型 約束,
例:
->id int(10) not null primary key,
->name varchar(20) not null);
unsigned 不允許空間出現負數,可以使空間增加一倍,只能用於數值型
例如
id int(10) unsigned not null primary key auto_increment
主鍵及自增
id int(10) not null primary key auto_increment
向名中增加字段
alter table 表名 add 字段名 類型 其他
修改字段類型
alter table 表名 change 列名 列名 類型
更改列名
alter table 表名 change 原列名 更改後的列名
查看字段
desc 表名
清空表: delete from 表名;
刪除表:drop table [if exists] 表名;
刪除數據庫:drop database [if exists] 庫名;
向表中添加一條記錄:insert into 表名(列名1,列名2)values(**,**);
向表中添加多條記錄:insert into <表名>(列名1,列名2)
values(**,**),
(**,**),
(**,**),
(**,**);
更改記錄操作:update 表名 set 列名=更新值 where 更新條件;
例如:update student set sname="Tom" where sname="Alex";
刪除記錄操作:delete from 表名 where 刪除條件
例如:delete from student where sage<18;
查詢記錄操作:select 列名 from 表名 where 查詢條件 order by 列名 asc/desc;
例如:select * from student;
使用集函數:count(列名) 計算元素的個數
sun(列名) 對某一列值求和,但必須是整數
avg(列名) 對某一列的值計算平均值
max(列名) 找出某一列的最大值
min(列名) 找出某一列的最小值
例如:1.查詢學生總數
select count(*) from student;
2.查詢選修課程的學生人數
select count(distinct studentid) from sc;
3.查詢1號課程的學生平均成績
select avg(grade) from sc where courseid=1;
4.查詢1號課程的學生最高分和最低分
select max(grade) as '最高分',min(grade) as '最低分'
from sc where courseid=1;
5.查詢每個學生的平均成績
select studentid,avg(grade) as '平均成績' from sc
group by studentid ;
6.查詢學生的平均成績在70分以上的
select studentid,avg(grade) as '平均成績' from sc
group by studentid having avg(grade)>70;
、、、、、、、、、、、連接數據庫
/**
*
*/
import java.sql.*;
/**
* @author Administrator
*
*/
public class kk {
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自動生成方法存根
try {
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/myschool","root","123456");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from user");
while (rs.next()) {
int id=rs.getInt("id");
String name=rs.getString("name");
int score=rs.getInt("score");
System.out.println("學號:"+id+",姓名:"+name+",分數"+score);
}
} catch (Exception e) {
// TODO: handle exception
System.out.println("故障"+e.toString());
}
}
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" import="java.sql.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'Login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<form action="Mainbody.jsp",method="post">
<label>用戶名:</label><input type="text" name="user" />
<label>密碼:</label><input type="password" name="pwd" />
<input type="submit" value="登錄" />
</form>
</center>
<%
String user1="";
String pwd1="";
try{
String users=request.getParameter("user");
String pwds=request.getParameter("pwd");
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/myqq","root","123456");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from stu");
while(rs.next()){
user1=rs.getString("user");
pwd1=rs.getString("pwd");
}
if(!users.equals(user1)||!pwds.equals(pwd1)){
out.print("請輸入正確的用戶名或密碼");
}
if(users.equals(user1)&&pwds.equals(pwd1)){
response.sendRedirect("Mainbody.jsp");
}
}catch(Exception ex){
System.out.print("系統錯誤"+ex.getMessage());
}
%>
</body>
</html>