zoukankan      html  css  js  c++  java
  • 数据库-表

    数据库-表

    修改数据库,只能修改字符集,不能修改库名

    alter database 库名 default character set gbk;

    gbk与utf8都是中文码表,区别在于gbk一个汉字2字节,uft8一个汉字3字节

    系统默认码表是gbk

    表 table

    use 库名;(用来选择数据库)

    create table 表名(

      字段名1 类型,

      字段名2 类型

    );

    数值类型:

    int:整数类型(年龄等使用)

    float:浮点型 double:精度高,可存整数类型(成绩钱等使用)

    文本类型:

    char:固定长度字符串,不够的用空格补齐

    varchar:可变长度字符串(可以声明长度)(QQ号手机号身份证号等使用)

    时间日期:

    date:只存年月日(生日字段)

    datetime:年月日时分秒(用于订单的生成时间)

    timestamp:时间戳

    创建表前要先使用use database语句

    create table student(

      sid int,

      sname varchar(10),

      ssex char(5),

      sbirthday date,

      grade double

    );

    查看表结构:desc 表名;

    修改表:主要是对字段和表名进行操作

    1.添加字段

    alter table 表名 add column 字段名 字段类型;

    alter tablestudent add column saddress varchar(30);

    2.删除字段

    altertable 表名 drop column 字段名 字段类型;

    alter table student drop column saddress varchar(30);

    3.修改字段类型

    alter table student modify column 字段名 字段类型

    alter table student modify column sbirthday date;

    4.修改字段名称

    alter table 表名 change column 字段名 字段类型

    alter table student change column grade score int;

    5.修改表名称

    alter table 表名 rename to 新表名

    alter table student rename to user;

    6.删除表

    drop table 表名

    drop table user;

    表是结构的一部分,修改用alter,删除用drop,创建用create,展示用show,添加用add

    表数据只能一行一行添加,要有整体意识,修改只能用update修改

    1.新增数据

    新增全部字段

    insert into 表名 values(按顺序添加数据)(insert执行一次添加一次新数据)

    新增部分字段

    insert into 表名(字段名1,字段名2)values(值1,值2)

    2.查询数据

    select * from 表名

    select * from student

    3.修改数据(带条件修改)

    update 表名 set ssname=‘’ where sid=‘’;

    修改多个字段

    update 表名 set ssname=‘‘,ssex=’‘ where sid=‘’;

    4.删除数据

    delete from 表名 where sid=1;(所在位置 带条件删除)

    delete from 表名 (全表删除 清空表 可以找回 只能删除标的数据,不能删除表的约束)

    truncate table 表名 (清空表 相当于直接从硬盘删除数据,不能恢复 数据和约束全删 )

    查询数据

    1.查询所有字段 select * from 表名;

    2.查询指定字段

    select sid,ssex from 表名;

    3.查询时添加常量列

    select sid as ’编号‘,sname as ’姓名‘ from 表名;

    4.查询时和并列

    select sid,sname,jsp+html as ssum from score

    5.查询去除重复记录

    select distinct(ssex) from student          去除表student中ssex字段的重复项

    select distinct ssex from student (不能和普通字段一起查询,一般只查一个字段)

    6.条件查询

    逻辑条件 and与 or或

    查询性别为男并且成绩及格同学

    select * from student where ssex=’男‘ and score>60;

    查询性别为男并且成绩不及格同学生日

    select sbirth from student where ssex=’男‘ and score>60;

    7.比较条件

    <>不等于 between and 介于(大于等于,小于等于才可以用,专于用于表达一个范围) 大于 小于

    8.判空条件

    null是真的空,什么都没有 值为空

    查询所有姓名不为空的同学信息

    select * from student where sname<>''; select * from student where sname is not null and sname <>'';

    9.模糊条件查询:like

    %:表示任意个字符      _:表示一个字符

    查询所有姓章同学信息

    select * from student where sname like ’章%‘;

  • 相关阅读:
    C语言读写伯克利DB 4
    程序之美(转自知乎)
    C语言读写伯克利DB 3
    ON DUPLICATE KEY UPDATE
    nanomsg:ZeroMQ作者用C语言新写的消息队列库
    新浪研发中心: Berkeley DB 使用经验总结
    [企业开源系列]后起之秀Facebook凭什么挑战互联网霸主Google?
    BZOJ1770:[USACO]lights 燈(高斯消元,DFS)
    BZOJ5293:[BJOI2018]求和(LCA,差分)
    BZOJ5301:[CQOI2018]异或序列(莫队)
  • 原文地址:https://www.cnblogs.com/xinzong/p/14241380.html
Copyright © 2011-2022 走看看