MySQL操作

简介

SQL server 微软 功能强大
oracle 稳定
SQLite 轻量级
Mysql 免费
原子操作:最小级别不能再细分的操作
事物就是原子性操作:举个经典例子,银行转钱,a减少钱,b加钱。为一个事物,是一个原子性操作。如果a减少了,断网断电了,b没加钱,a有网之后,事物回滚到a原来的钱数。

安装 

1下载zip包
2解压到任意目录并把bin加入环境变量
3运行服务端 mysqld-------失败(因为还没有初始化)
4 在mysql目录下新建一个data文件夹
5 初始化命令:用管理员身份运行cmd--cd到mysql目录下的\bin目录 mysqld--initialize-insecure    data目录就会创建出很多用户相关的数据 和mysql架构相关的东西  同时创建了一个用户名root的用户,没有密码
6 在bin目录下执行mysqld(就是server客户端),程序hang住了,其实就是一个socket 等待客户端来连接(客户端可以有很多 python java PHP等都可以连这个socket) MySQL内部自己也做了一个客户端
7另外重新启动一个cmd 在bin目录下执行mysql(就是client客户端):mysql -u root -p(这就是刚才mysqld --initialize-insecure初始化的那个用户)
8成功连接并进入mysql
9创建一个mysql服务—— 管理员身份进入bin目录之后再执行命令 mysqld --install (如果直接执行则是在c盘默认搞一个路径,不是mysqld的真实路径,所以会失败)     任务管理器-服务-MySQL-启用
10在cmd中开启、关闭MySQL链接   net start/stop mysql

 

一、初试

show databases;
create database db1;
use db1;
show tables;
create table tb1(nid int,name varchar(20));
insert into tb1(nid,name) values (1,'andy');
select * from tb1;
desc tb1

二、database和table什么时候有什么时候没有

带database
show databases  create database 
不带
use db1  

带table  
show tables  create table  drop table  alter table  truncate table
不带table
select * from tb1  delete from tb1  insert into tb1()  update tb1 set name='ka',age=2 where nid>3  desc tb1

三、数据库操作

1查看数据库  show databases;
2创建数据库  create database db1 default charset utf8 collate utf8_general_ci;
          
1.default charset=utf8(=可以没有,用空格代替)
          2. collate utf8_general_ci 内部对比排列各种语言文字的机制;
3使用数据库  use db1;

四、数据表操作

1显示表  

show tables;

2创建表  

create table tb1(nid int not null default 1,name varchar(20) ) engine=innodb default charset=utf8;  
create table tb1(nid int not null auto_increment primary key,name varchar(20) ) engine=innodb default charset=utf8;  
  (=可以没有,用空格代替)这个引擎支持原子性操作事物
  1 不可以为空not null ,不写默认可以为空null
  2 默认值 1default 1
  3自增 (一张表只能有一个,必须是数字,必须是索引)nid int not null auto_increment primary key
    4主键,一种特殊的唯一索引,不允许有空值,如果主键使用单个列,则它的值必须唯一,如果是多列,则其组合必须唯一。

create table tb1(
                nid int not null auto_increment primary key,
                num int null
            )
            或
            create table tb1(
                nid int not null,
                num int not null,
                primary key(nid,num)
            )
View Code    
create table tb1(
                nid int not null auto_increment primary key,
                num int null
            )
            或
            create table tb1(
                nid int not null,
                num int not null,
                primary key(nid,num)
            )
View Code
  5唯一索引:(一张表可以有多个,可以为null,)1约束 2索引,加速查找

      insert into tb1(nid,name) values (1,'andy');

      select * from table1;

  6外键:foreign key,一对多(一个部门可能对应多个员工),两张表建立约束

  在表外创建

    alter table 从表名 add constraint fk_从表名_主表名 foreign key 从表(外键字段) reference 主表(主键字段); 

  在表内创建

    constraint fk_u_p foreign key (外键字段)  reference 主表名(主键字段);

三、删除表   

    drop table tb1;                 #删除表
    truncate table tb1                #清空表 自增恢复到1,性能高,速度快(比delete)
    delete from tb1;                #清空表 自增不恢复到1

四、修改表

  alter详细数据行列级别

 
添加列:alter table 表名 add 列名 类型
删除列:alter table 表名 drop column 列名(column不加也行)
修改列:
        alter table 表名 modify column 列名 类型;  -- 类型
        alter table 表名 change 原列名 新列名 类型; -- 列名,类型
  
添加主键:
        alter table 表名 add primary key(列名);
删除主键:
        alter table 表名 drop primary key;
        alter table 表名  modify  列名 int, drop primary key;
  
添加外键:alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);
删除外键:alter table 表名 drop foreign key 外键名称
  
修改默认值:alter table testalter_tbl ALTER i SET DEFAULT 1000;
删除默认值:alter table testalter_tbl ALTER i DROP DEFAULT;

  update 详细看数据行列级别

五、查看表

    select * from tb1; #查看表tb1中所有数据
    desc tb1;     #查看有哪些列及属性

五、数据行(列)级操作

    insert into tb1 (age,name)values (18,'andy');                #增加一行

    insert into tb1 (age,name)values (80,'andy')),(20,'andy')),(30,'andy'));  #增加多行 

    insert into tb1 (age,name)select age,firstname from tb2           #从tb2中选择数据增加到tb1

    delete from tb1 where nid=1 and name=‘andy’;   #删除nid为1   且   name为andy的数据行

    delete from tb1 where nid=1 or name=‘andy’;    #删除nid为1   或   name为andy的数据行

    delete from tb1 where nid>1;           #删除nid大于1的数据行

    delete from tb1 where nid<1(>=   <=   !=);      #同上 小于 小于等于 不等于

改  update(列内容)

    update tb1 set age =2 and name =‘andy’ where nid=2;        

    update tb1 set age =2 and name =‘andy’ where nid<2(或者nid>5)and age<=15 or name='eric';     
  

     alter(列名 列类型)

    select * from tb1;                        #查看表tb1中所有数据

    select * from tb1 where nid>2;                   #查看表tb1中nid>2的数据

    select name,age,nid from tb1 where nid>2 and age>5;  (按照此先后顺序给出)的数据  !!!!!!! 都写一遍比select*的效率高

    select name from tb1 where nid between 2 and 5;        #nid在2和5之间的name列

    select name from tb1 where nid in (1,5,8);        #nid是1,5,8的name列

     select name from tb1 where nid not in (1,5,8);      #nid是1,5,8的name列

    select name from tb1 where nid in (select nid from tb2);    #查看表tb1中nid是tb2中nid列的数据 的name列   

 

列操作:

    增      alter table tb1 add 列名 类型;

    删      alter table tb1 drop column 列名;

    改

      alter tbale tb1 modify 列名 类型;              #只改类型

      alter table tb1 change column 原列名 新列名 类型;   #列名,类型  

posted @ 2017-12-02 16:25  Andy__li  阅读(155)  评论(0)    收藏  举报