MySQL基础

来源:互联网 发布:淘宝商城电脑版 编辑:程序博客网 时间:2024/05/17 04:43
数据库基本操作:
##############################################################
1,mysql 连接基本命令:
Mysql -h localhost -u root -p 默认连接主机可以省略-h localhost
Mysql -u root -p

2, 查看当前数据库中有哪些库:
show databases;

3,创建一个库:
create database db_user;

4,切换数据库:
use db_user;

5,查看数据库下面的表:
show tables;

6,删除一个库:
drop database db_user;

7,mysql数据库名字不能修改

8,删除一张表:
drop table tableName

9,创建一张表
 create table class
 (
    stu int,
    name varchar(20),
    age int,
    area varchar(20)
 );

 10,修改表名:
 rename table oldName to newName;

 11,查看表结构:
 desc[ription] tableName;

+----+--------+------+------------+------+
| id | name   | sex  | birthday   | sal  |
+----+--------+------+------------+------+
|  1 | tanwei | 男   | 1992-08-20 | 4500 |
|  2 | 鸣人   | 男   | 1993-08-20 | 5000 |
+----+--------+------+------------+------+

mysql> desc emp;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| id       | int(11)     | NO   | PRI | NULL    | auto_increment |
| name     | varchar(12) | YES  |     | NULL    |                |
| sex      | char(4)     | YES  |     | NULL    |                |
| birthday | date        | YES  |     | NULL    |                |
| sal      | double      | YES  |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)

12 创建一张关于留言板的表:
create table message
(
    id int,
    title varchar(60),
    name varchar(10),
    content varchar(1000)
);

表的基本操作:
##############################################################


首先解决字符集问题:
默认建表

1,新增一条数据
insert into message(id, title,name,content)
values(10086,'初来乍到','Tanwei','刚来能不能当老大?');

2,修改表的数据
update message
set
id=10080,
content='偏要当老大'
where
name='Martin';

修改列的类型
 alter table rj1001 modify startdate date not null  default '2012-01-01';

3,一行语句添加多条数据

insert into message(id,title,name,content)
values
(10010,'桃园','刘备','雌雄双剑'),
(10020,'三','关羽','青龙偃月刀'),
(10030,'结义','张飞','丈八蛇矛');

4,删除表中的一条数据
delete from message where id='10080';

5,查询所有行所有列
select * from message;

6,查询部分行部分列
select name,content from message where id>2;

##############################################################

详解列类型

整形:

Tinyint/smallint/mediumint/int/bigint
    
 类型          字节        位       无符号           有符号
Tinyint         1            8        0~255        -128~127

Smallint    2           16        0~65535        -32768~32767

Mediumint       3           24        0~2^24-1        -2^23~2^23-1

Int            4           32        0~2^32-1        -2^31~2^31-1

Bigint            8           64        0~2^64-1        -2^63~2^63-1


创建表:
create table message
(
    id int primary key auto_increment,
    name varchar(10),
    age tinyint
)
charset utf8;

alter table message add age2 tinyint unsigned;


7,整形列的可选属性:
tinyint(M) unsigned zerofill;
M:宽度(在0填充的时候才有意义)
unsigned:无符号类型(非负)
zerofill:0填充,(默认无符号)


8,给列添加默认值:
mysql> alter table message add age4 tinyint not null default 0;


9,小数型(浮点型)
Float(M,D) M:精度(总位数) D:标度(小数位)  float(6,2) -9999.99~9999.99

create table goods
(
    name varchar(10) not null default '',
    price float(4,2)  not null default 0.00
) charset utf8;

10,定点型
Decimal:进行精确运算、
浮点和定点在计算机中都用4个字节或8个字节

11,字符串类型
Char:定长字符串,char(M),M代表宽度,即可容纳的字符数
Varchar:变长字符串,varchar(M),M代表宽度,即可容纳的字符数
速度上:定长要快些

Char型与Varchar的选择原则:
①空间利用效率 ,四字成语表,char(4),
个人简介,微博Varchar(140)
②速度
用户名:char
text :文本串 越2w-6w个字符, 但是不支持全文索引

create table t2
(
    gender enum('男','女')
);

12,日期时间类

 年 --> year

 年-月-日 -->date

 时间 -->time

 年-月-日 hh:mm:ss -->datetime

Year类型:一个字节,1901-2155年
如果输入2位,'00-69'表示2000-2069
         '70-99'表示1970-1999

Date类型:典型格式 1992-08-12
日期类型:'1000-01-01'-->'9999-12-31'


Date类型 典型格式 '1992-08-20 14:35:59'
日期类型:范围'1000-01-01 00:00:00'-->'9999-12-31 12:31:59:59'


mysql> create table user(
    ->
    -> name varchar(20) not null default '',
    -> regtime datetime not null default '1000-01-01 00:00:00')charset utf8
    -> ;
Query OK, 0 rows affected (0.01 sec)

mysql> desc user;
+---------+-------------+------+-----+---------------------+-------+
| Field   | Type        | Null | Key | Default             | Extra |
+---------+-------------+------+-----+---------------------+-------+
| name    | varchar(20) | NO   |     |                     |       |
| regtime | datetime    | NO   |     | 1000-01-01 00:00:00 |       |
+---------+-------------+------+-----+---------------------+-------+

注意:在开发中,很少用日期时间类型来表示一个需要精确到秒的列
    原因:虽然日期时间类型能精确到秒,而且方便查看

    用时间戳表示
    
       //在java中用时间戳转化时间
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String strDate=row.get("AssignTime").toString();
    Date date=sdf.parse(strDate);
    Timestamp sd=new java.sql.Timestamp(date.getTime());
    assign.setAssignTime(sd);

建表

给班级建立一个档案管理表方便联系

如下信息:


姓名:char(4)
年龄:tinyint unsigned
email: varchar(30)
手机:char(11)
简介:varchar(1000)
毕业薪水:decimal(7,2)
毕业日期:date

create table rj1001
(
  id int primary key auto_increment,
  name char(4) not null default '',
  age tinyint unsigned not null default 0,
  email varchar(30) not null default '',
  tel char(11) not null default '',
  salary decimal(7,2) not null default '2000.00',
  startdate date not null default '2012-03-13'
)charset utf8;


########################################################
数据库的增删改查

1,增加数据
mysql> insert into rj1001(name,age,email,tel,startdate)
    -> values ('刘备','99','liubei@shuguo.com','13800138000','2012-12-26');
Query OK, 1 row affected (0.30 sec)

mysql> select * from rj1001;
+----+------+-----+-------------------+-------------+---------+------------+
| id | name | age | email             | tel         | salary  | startdate  |
+----+------+-----+-------------------+-------------+---------+------------+
|  1 | 刘备 |  99 | liubei@shuguo.com | 13800138000 | 2000.00 | 2012-12-26 |
+----+------+-----+-------------------+-------------+---------+------------+



//当不指定列名时,需要填写自增长的id值,这里与SQLServer不同
mysql> insert into rj1001 values(2,'关羽',88,'aaaa@sa',12345678909,1234.00,'2012-01-02');
Query OK, 1 row affected (0.05 sec)

mysql> select * from rj1001;
+----+------+-----+-------------------+-------------+---------+------------+
| id | name | age | email             | tel         | salary  | startdate  |
+----+------+-----+-------------------+-------------+---------+------------+
|  1 | 刘备 |  99 | liubei@shuguo.com | 13800138000 | 2000.00 | 2012-12-26 |
|  2 | 关羽 |  88 | aaaa@sa           | 12345678909 | 1234.00 | 2012-01-02 |
+----+------+-----+-------------------+-------------+---------+------------+


//一条语句同时插入多条值
insert into rj1001 (name,age,tel)
values
('张飞',79,'110'),
('赵云',69,'111'),
('黄忠',109,'112'),
('马超',59,'113');

2,修改表的数据
update rj1001 set email ='machao@xiliang.com',salary=3999.34
where id=6;


3,删除表中的一条数据
 delete from rj1001 where id =5;

 ##############################################
 查询

 select 5中子句介绍

 where 条件查询
 group by 分组
 having 筛选
 order by 排序
 limit 限制结果条数


group by 和聚合函数的使用:
 
select cat_id,max(shop_price) from goods by cat_id;

//区分大小写的别名和带空格的别名 需要加上双引号



//存储过程

DELIMITER $$

DROP PROCEDURE IF EXISTS `test`.`AddUser` $$
CREATE PROCEDURE `test`.`AddUser` (in pname varchar(45), in birthday date, in money float,out pid int)
BEGIN
     insert into  rj1001(name ,birthday,money) values(pname,birthday,money);
     select last_insert_id() into pid;
END $$

DELIMITER ;