zoukankan      html  css  js  c++  java
  • 数据库基本操作与数据类型

    mysql数据库库的操作

    '''库操作'''
    show databases; 	#查询所有的数据库
    create database student; 	#创建一个student的库
    create database db1 charset utf8, 	#创建一个db1的库',并指定编码
    use student; 	#进去student这个库,(然后才能操作表)
    drop database student;	#删除student这个库(慎用)
    

    mysql数据库的表操作

    1:#创建一个叫t1的表,申明引擎为innoDB
        create table t1(id int,name char(16))engine=innodb;	
    2:#查看库下面的所有表
        show tables;	
    3:#查看t1的表结构(desc全称describe)
        desc t1;	
    4:#查看创建表的详细详细(创建语句,编码等)
        show create table t1;	
    5:#删除t1这个表
        drop table t1;	
    6:#修改表的名字
        alter table t1 rename student;
    7:#修改表的数据类型
        alter table t1 modify name char(16);
    8:#修改表的字段名和数据类型
        alter table t1 change name name1 char(15);
        alter table t1 change name name1 旧数据类型;
        '''
        change比modify多了一个修改字段名的功能,如果change后面要修改的数据类型使用的还是旧的		数据类型,那么就只修改了字段名,如果写的新字段名和就得字段名一样,就和modify一样只是修改	   了数据类型
        '''
    9:#给t1这个表增加一个字段
    	alter table t1 add name char(10) first;
    	alter table t1 add age int unsigned ,add sex enum('男','女')default '男';
    	alter table t1 add salary float(7,2) not null after name;
        '''
        添加字段时,可以使用逗号分割一次添加多个字段,first是表示将新添加的字段放到第一个字段的	  位置去(类似于在表第一个字段前面插入一个字段),after表示的是将新添加的字段放到指定的字	段后面,上面的例子就是把salary这个字段加到name的后面,有了这两个字段就可以将新增加的字	段放到任意一个位置
        '''
    10:#给t1这个表中的字段bname添加一个约束条件
    	alter table t1 add unique(bname);
    11:#把t1这个表中的主键取消
    	alter table t1 drop primary key;
    12:#把t1这个表中bname字段的unique这个约束条件取消
    	alter table t1 drop index 'bname';
    13:#删除表中的一个字段
    	alter table t1 drop name;
    14:#复制表(了解就好)
        create table student select * from service;
        create table student select * from server where 1=2;只复制表结构
        '''
    	这句话的意思是从service这个表里查询到的数据直接给了student这个要创建的表,表结构和数	 据是一样的,但是不能复制主键,外键和索引,可以使用alter 给他加回来
        '''
    15:#删除外键关系
        alter table book drop foreign key book_ibfk_1(外键名称);
    16:#创建表完成之后,后添加外键关系
    	alter table book add foreign key(pid) references publish(id);
    17:#创建外键时指定外键名称
        #创建表时:
    	create table t1(id int,pid int,constraint fk_t1_publish foreign key(pid) 		references publish(id);)
    	#创建表完成之后,后添加外键关系
    	alter table book add constraint fk_t1_publish foreign key(pid) references 		publish(id);
    

    MYSQL数据简单操作

    '''数据操作,简单操作,详细的看下面的数据库操作'''
    1:#查看student这个表中的所有数据(*代表所有)一般少用*,因为查询效率低
        select * from student;
    2:#往student表中添加两条数据,还可以指定字段添加数据,可以插入一条和多条,用逗号分割
        insert into student values(1,'田彩'),(2,'李杰');
        insert into student(name) values('田彩'),('李杰');
        insert into student(id,name,age)select (id,name,age)from t1;
        '''
        insert 可以同时插入多条数据,也可以指定数据插入,还可以讲查询到数据插入到一张表中,上面
        最后一个示例就是把t1表中查询到的三个字段的数据,插入到student表中对应的字段中,但是这种
        插入数据的方式需要字段和数据类型等意义对应好
        '''
    3:#将id为1的name改成田少岗
        update student set name='田少岗' where id=1;
    4:#将id为1的name改成田少岗,id改成2可以指定修改多个字段的值,用逗号分割就好
        update student set name='田少岗' id=2 where id=1 
    5:#把student这个表的数据清空(慎用),删除后插入数据还是从以前最后一个id的位置开始,
        delete from student;
    6:#把student这个表中id为1的数据删掉(如果有自增id,新增的数据,仍然是以删除前的最后一样作	为起始。)
        delete from student where id = 1;
    7:#数据量大,删除速度比上一条快,且id直接从零开始,
        truncate table t1;
    

    Mysql支持的数据类型

    • 数值

      1:tinyint(1个字节)小整数型(0,255)(-128,127)tinyint unsigned(指定使用正数,不能有符	号)#*********
      2:int(4个字节)	大整数型(0,4294967295)int unsigned(指定使用正数,不能有符号)#******
      3:float 单精度浮点数值 (4个字节)float(10,5)10代表这个数据一共几位,5表示小数点后面保留	几位,最大(255,30)一般情况下不指定范围,小数点后面只保留5位,不是很准确,double也是,	  在能保留的最后一位小数上会四舍五入#***********
      4:double 双精度 浮点数 (8个字节)
      5:decimal(65,30)可以精确到小数点后面30位,不写参数,默认是10位的整数
      
    • 时间

      1:datetime 年月日时分秒 ******(可以把timestamp的属性写到后面.抄过来,show)
      2:date	   年月日   ******
      3:time	   时分秒   ***
      4:year	
      5:timestamp(约束不能为空,默认当前时间,在修改表数据时同时更新时间)
      #now()获取当前时间
      存储时间时,可以写字符串('2019-8-29 12:01:20').也可以写数字类型(201908291026)但是不能少位数
      
    • 字符串

      1:char (0-255个字节) 定长存储 设置几个就占几个位置,char(10)输入一个'田彩',也占10位,
          存储速度快,占用空间(默认为一)
      2:varchar (0-65535个字节) 变长存储 只存数输入的长度,比如输入'田彩',站的位数是2+1,这个	1是数据的长度,存储速度慢,占用空间小(必须指定长度 )
         # 记住这两个就可以了
      
    • set和enum

      1:enum enum('男','nv'),单选,只能选择括号里的选项
      2:set  set则是多选但是也必须是括号里的内容,
      
  • 相关阅读:
    angualrjs2教程
    需要关注的技术
    webstorm 2016
    Java内存泄露分析和解决方案及Windows自带查看工具
    2018-6-21-随笔-WEB应用程序
    2018-6-20-随笔-SQL Server中乱码
    2018年6月15日随笔--统计图
    2018-6-12随笔-类库
    2018-6-11随笔-返回值-密钥
    vs各种版本
  • 原文地址:https://www.cnblogs.com/luckinlee/p/11621139.html
Copyright © 2011-2022 走看看