签到天数: 2090 天 连续签到: 2 天 [LV.Master]伴坛终老IIII
|
楼主 |
发表于 2009-7-18 22:04
|
显示全部楼层
MySQL使用数据
MySQL使用数据
插入数据
insert into table-name (field-name, field-name, …) values (field-value, field-value, …)
insert into table-name values (field-value, field-value, …)
注意,第2中形式只能插入所有字段,而且要与字段定义顺序保持一致,而第一种形式则可以插入部分字段,且其顺序保持和前面的field-name顺序一致即可
插入时可以插入计算表达式或者是函数
insert into marks (name, math, sci, total) values (‘James Doe’, 45, 67, math + sic);
insert into users (id, name, password) values(last_insert_id() + 1, ‘tom’, ‘secret’);
如果插入的是字串,则需要用引号,如果含有特殊字符,则需要使用转义字符, 例如:要插入Bob’s ,则使用” Bob\\’s”
插入时into关键字可以省略
还可以使用insert into table-name values (v1,…), (v2,…), (v3,…) , …来插入多条数据
还可以使用insert into table-name set field=value, …这种形式来插入
insert into stocks set symbol=’HYDH’, price=2000, auantity=29;
如果某字段有默认值,则可以使用default来使用默认值。
insert into addressbook values (4, 'Apache', 'Books', '234 8623', '234 6232', 'apache@some.domain', default);
如果没有设定该字段,且该字段有默认值,则数据库会自动使用默认值
insert into addressbook (id, fname, lname, phone, fax, email) values(5, ‘Tomcat’, ‘Books’, ‘223 8928’, ‘234 8362’, ‘tom@some.domain’),此时web_site字段会自动使用默认值
当使用unique约束时,使用ignore可以将违反约束的行忽略,不进行提示,不插入
insert ignore into menu(id, label, url) values (4, ‘Concact Us’, ‘contactus.html’);
insert into menu (id, label, url) values (4, ‘Contact Us’, ‘contactus.html’) on duplicate key update label = ‘ContactUs’, url=’contactus.htm’
更新
update table-name set field-name = field-value, field-name = field-value, …where …
如果没有where子句,将导致整个表被更新
删除
delete from table-name where condition-test
如果没有where子句,将导致整个表被删除
delete from users;将表users中所有记录删除
truncate table users将表users清空
如果想清空表,truncate要比delete快
检索
select * from users limit 0, 2
表示从user中取从0开始的2条数据, 注意索引是从0开始
select * from users limit 2;和上边的一样,取两条数据
select * from users limit 2, -1; 从第3条开始到结束的数据
select * from users order by name, age; 先以name排序,再以age排序
select * from users order by name desc;以name倒叙排序,asc表示正序,默认排序
变量
变量赋值 :=完成,如@oldest := min(age);
只在客户会话期间存在,
select @oldest := max(age) from users;
select @oldest 可以显示@oldest这个变量
select * from users where age >= @oldest;
注意,变量名区分擦小写,且只能包含字母,数字字符,下划线和点
控制select行为
Distinct 删除重复记录,仅保留重复记录中的一条
SQL_CACHE和SQL_NO_CACHE指定MySQL是否使用高速缓存
导入数据
load data infile可以用于从文件中导入数据
load date infile ‘F:/infile.sql’ into table birthdays fields terminated by ‘,’ lines terminated by ‘\\r\\n’
注意:默认情况下,MySQL认为文件在服务器,如果想使用客户端的数据,可以使用关键字local
load data local infile ‘f:/data.txt’ into table birthdays fields terminated by ‘,’ lines terminated by ‘\\r\\n’
如果数据字段少于表的字段,可以指定填充的字段
load data infile ‘f:/data.txt’ into table addressbook fields terminated by ‘ ‘ enclosed by ‘”’ lines terminated by ‘\\r\\n’
load_priority使在进行导入之前,服务器等待直到没有其他线程使用该表
concurrent允许可以在导入时从表中读取数据
load data low_priority infile ‘log.asc’ into table addressbook
load data concurrent local infile ‘f:/data.txt’ into table addressbook
ignore使数据在导入时如果新数据与原来数据发生主键重复,则会跳过该数据,继续执行以后的数据导入
load data infile ‘users/txt’ into table usrs lines terminated by ‘\\r\\n’ (uname)
load data infile ‘usrs/txt’ ignore into table users lines terminated by ‘\\r\\n’ (uname)
load data infile ‘usrs/txt’ replace into table users lines terminated by ‘\\r\\n’ (uname)
导出数据
select … into outfile ‘f:/addressbook.txt’将addressbook的数据导出到addressbook.txt文件中
导入和导出数据都有关键字lines terminated by 指定行分隔符,默认是\\n
Fields指定字段分隔符,其后必须跟一个或者多个关键字,terminated by或者escaped by, enclosed by。
terminated by 指定分隔符,默认是\\t, escaped by 指定跳过的特殊字符,默认是反斜线,enclosed by 指定包围字段值的符号,无默认值
如果向文件中写二进制文件,则使用into dumpfile来代替into outfile,此二命令都将内容写入到服务器中的文件,且要写入的文件不能存在
以下格式可以达到同样的效果
select *into outfile ‘f:/users.txt’ fields terminated by ‘,’ optionally enclosed by ‘”’ lines terminated by ‘\\r\\n’ from users |
|