MYSQL
两篇博客:
http://www.cnblogs.com/tiantianbyconan/archive/2012/07/08/2581684.html
http://www.cnblogs.com/mr-wid/archive/2013/05/09/3068229.html#c6
1.执行脚本命令:source E:\123.sql(路径名/xxx.sql)
2.数据类型, 分别为数字、日期\时间、字符串, 这三大类中又更细致的划分了许多子类型:
- 数字类型
- 整数: tinyint、smallint、mediumint、int、bigint
- 浮点数: float、double、real、decimal
- 日期和时间: date、time、datetime、timestamp、year
- 字符串类型
- 字符串: char、varchar
- 文本: tinytext、text、mediumtext、longtext
- 二进制(可用来存储图片、音乐等): tinyblob、blob、mediumblob、longblob
3.创建一个数据库
create database 数据库名 [其他选项];
use 数据库名
4.创建数据库表
create table 表名称(列声明);
5.操作MySQL数据库
(1)向表中插入数据
insert [into] 表名 [(列名1, 列名2, 列名3, ...)] values (值1, 值2, 值3, ...);
insert into students values(NULL, "王刚", "男", 20, "13811371377");
(2)查询表中的数据
select 列名称 from 表名称 [查询条件];
指定查询条件, 用法形式为: select 列名称 from 表名称 where 条件;
where 子句不仅仅支持 "where 列名 = 值" 这种名等于值的查询形式, 对一般的比较运算的运算符都是支持的, 例如 =、>、<、>=、<、!= 以及一些扩展运算符 is [not] null、in、like 等等。 还可以对查询条件使用 or 和 and 进行组合查询, 以后还会学到更加高级的条件查询方式, 这里不再多做介绍。
示例:
查询年龄在21岁以上的所有人信息: select * from students where age > 21;
查询名字中带有 "王" 字的所有人信息: select * from students where name like "%王%";
查询id小于5且年龄大于20的所有人信息: select * from students where id<5 and age>20;
(3)更新表中的数据
update 表名称 set 列名称=新值 where 更新条件;
(4)删除表中的数据
delete from 表名称 where 删除条件;
6.创建后表的修改
alter table 语句用于创建后对表的修改, 基础用法如下:
(1)添加列
基本形式: alter table 表名 add 列名 列数据类型 [after 插入位置];
示例:
在表的最后追加列 address: alter table students add address char(60);
在名为 age 的列后插入列 birthday: alter table students add birthday date after age;
(2)修改列
基本形式: alter table 表名 change 列名称 列新名称 新数据类型;
示例:
将表 tel 列改名为 telphone: alter table students change tel telphone char(13) default "-";
将 name 列的数据类型改为 char(16): alter table students change name name char(16) not null;
(3)删除列
基本形式: alter table 表名 drop 列名称;
alter table students drop birthday;
(4)重命名表
基本形式: alter table 表名 rename 新表名;
alter table students rename workmates;
(5)删除整张表
基本形式: drop table 表名;
drop table workmates;
(6)删除整个数据库
基本形式: drop database 数据库名;
drop database samp_db;