您的位置:首页 > 数据库 > MySQL

最近在学习mysql数据库,一些记录总结

2013-04-04 22:40 393 查看
在window xp上使用mysql-installer-community-5.5.30.1.msi装了相应版本的mysql。可从http://dev.mysql.com/获取相应的安装包。本来想装5.6的版本,发现官网声明5.6版本不支持window xp,就没装。装的过程发现需要.net framework相关东西,直接在360的软件管理中安装了.net framework,安全方便。

安装完毕后,在

在开始-程序-mysql中启动mysql workbench,按照官网的介绍尝试了下面的命令,

using mysql work bench:

1,query operation:

select VERSION(),CURRENT_DATE;

select SIN(PI()/4),(4+1)*5;

select version();

select now();

select user();

2, Use the show statement to find out what databases currently exist on the server:

show databases;

3,create database:

create database bestree;

4,create a table:

show tables;

create table pet(name varchar(20),owner varchar(20),

species varchar(20),sex char(1),birth date,death date);

5,show tables;

describe pet;

6,

load data local infile 'E:\pet.txt' into table pet lines terminated by '\r\n';

7,

insert into pet values('bestree','best','fa','f','1988-3-4',NULL);

as you can see

String and date values are specified as quoted strings here.and

you can insert NULL directly to represent a missing value

8,select

select * from pet where name='bestree';

select * from pet;

select * from pet where birth>='1988-2-28';

select * from pet where (species='cat' and sex='m')or (species='dog' and sex='f');

9, select particular column

select name,birth from pet;

10,order by

select name,birth from pet order by birth;

感觉还不错。

后来想通过eclipse写java代码访问下数据库,于是百度了下,下面代码参考了百度的结果

public class Test {
public static void main(String[] args) {
String driver = "com.mysql.jdbc.Driver";
try {
Class.forName(driver);
// 连续数据库
Connection conn = DriverManager
.getConnection("jdbc:mysql://localhost:3306/bestree",
"bestree", "");

if (!conn.isClosed()) {
System.out.println("Succeeded connecting to the Database!");
}
// statement用来执行SQL语句
Statement statement = conn.createStatement();
// 要执行的SQL语句
String sql = "select * from pet";
ResultSet rs = statement.executeQuery(sql);
while (rs.next()) {
String name = rs.getString("name");
System.out.println(name);
}
rs.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {

}

}
}


运行上面代码之前,需要将mysql-connector-java-5.1.23-bin.jar包放置到classpath目录。

有尝试了下面的命令

1,查看目前选中的数据库:

select database();

2,查看当前数据库下的表:

show tables;

3,查看每个表的结构:

desc pet;

4,查看某个表的索引:

show index from pet;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: