zoukankan      html  css  js  c++  java
  • mysql数据库基本操作

    下载地址 http://www.mysql.com/downloads/mysql/

    端口号:3306 用户名:root 密码:自定义

    连接到MySQL服务器 >mysql -uroot -proot [-h127.0.0.1]

    断开连接 >exit 或者>quit

    命令

    显示MySQL中所有的数据库 > show databases;

    切换到mydb数据库 > use mydb;

    查看数据库中所有的表 > show tables;

    查看表结构 > desc t_user;

    查看数据库版本和时间 > select version(),now();

    创建数据库 >create database mydb;

    数据类型(整型)

    数据类型 无符号范围 有符号范围
    TINYINT 0~255 -128~127
    SMALLINT 0~65535 -32768~32767
    MEDIUMINT 0~16777215 -8388608~8388607
    INT(Integer) 0~4294967295 -2147483648~2147483647
    BIGINT 0~18446744073709551 615 -9223372036854775808~9223372036854775807

    数据类型(浮点型)

    数据类型 无符号范围 有符号范围
    FLOAT 0,(1.175 494 351 E-38,3.402 823 466 E+38) -3.402 823 466 E+38,1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E+38)
    DOUBLE (1.797 693 134 862 315 7 E+308,2.225 073 858 507 201 4 E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) 0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308)
    DECIMAL(M,D)    

     数据类型(字符型)

    数据类型 大小 用途
    CHAR 0-255字节 定长字符串
    VARCHAR 0-255字节 变长字符串
    TINYTEXT 0-255字节  短文本字符串
    TEXT 0-65 535字节 长文本数据
    MEDIUMTEXT 0-16 777 215字节 中等长度文本数据
    LONGTEXT 0-4 294 967 295字节 极大文本数据

     数据类型(日期时间型)

    类型 范围 格式
    DATE 1000-01-01~9999-12-31 YYYY-MM-DD
    TIME -838:59:59~838:59:59 HH:MM:SS
    DATETIME 1000-01-01 00:00:00~9999-12-31 23:59:59 YYYY-MM-DD HH:MM:SS
    TIMESTAMP 1970-01-01 00:00:00~2037年 YYYY-MM-DD HH:MM:SS

    创建表:

    create table stu(
    id int auto_increment,
    stuname varchar(20) not null,
    classid int not null,
    primary key(id)
    );

    create table cls(
    id int auto_increment,
    clsname varchar(20) not null,
    primary key(id)
    );

    添加数据:

    insert into stu (stuname,classid)values('lyl',2),('fj',2);

    insert into cls (clsname)values('java'),('c++');

    主键 • 在设计表时总是要定义表的主键 • 表的主键设计策略 • 任意两行都不具备相同的主键值 • 每行都必须具有一个主键值(主键不允许Null列) • 主键和业务无关,不更改,不重用 • 主键可以是一个列或者是多个列的组合 • 使用PRIMARY KEY(XXX)来声明一个主键列 • 如果使用多个列作为主键则需要如下声明:PRIMARY KEY(XXX,XXX)

    主键自动增长 AUTO_INCREMENT • 用来标示一个自动增长列 • 一个表中只允许有一个自动增长列 >create table t_student ( id int auto_increment, … );

    删除表 > drop table t_student;

    非null约束 stuname varchar(20) not null

    默认约束 stuaddress varchar(100) default '郑州'

    唯一约束 stuname varchar(20) not null unique

    更改表

    添加一列 >alter table t_student add tel char(20);

    删除一列 >alter table t_student drop column tel;

    添加唯一约束 >alter table t_student add constraint uk_username unique(usercode);

    添加主键约束 >alter table t_user add constraint pk_t_user_id primary key t_user(id);

    添加默认约束 >alter table t_user alter password set default '123456';

    添加非null约束 >alter table t_teacher modify column uname varchar(20) not null;

    重命名表 >rename table t_student to t_stu

    导出数据库 >mysqldump -hlocalhost -uroot -proot mydb>C:/a.sql

    批量导入SQL脚本 >source C:/a.sql

    运算符

    逻辑运算符 • = 等于 • <>,!= 不等于 • < 小于 • > 大于 • <= 小于等于 • >= 大于等于 • between 在指定的两个值之间

    关系运算符 • and • or • not

    update >update t_student set stuname = 'Alex',age = '26' [where id = 1];

    where • where stuname = 'tom' • where stuname = 'tom' or stuname = 'alex' • where id > 1 and id < 3 • where id != 23 • where id = 12 or id = 34 • where id in (12,34) • where id between 12 and 34 • where password is null • where password is not null

    delete >delete from t_student [where id = 1];

    truncate >truncate table t_student; TRUNCATE TABLE用于删除表中的所有记录,但该语句不能包含WHERE语句,该操作运行速度比 DELETE语句快

    创建外键 >alter table t_user add schoolid int; >alter table t_user add constraint fk_student_cus foreign key(schoolid) references t_school(id);

    删除外键 > alter table t_user drop foreign key fk_student_cus

    基本查询 查询所有的列 >SELECT * FROM vendors;

    查询指定的列 >SELECT vend_id,vend_name,vend_address,vend_city FROM vendors;

    如果查询时需要显示表中的所有列,尽量避免使用通配符(*),而要采用写出所有列名的方式进行查询, 因为采用通配符查询会降低程序的查询性能

  • 相关阅读:
    vue 项目中assets文件夹与static文件夹引用的区别
    v-on绑定特性命名带小横杠 ‘-’与props属性中变量怎么对应
    解决 The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
    解决win10无法完成更新 正在撤销更改
    Felix HttpServer call iPojo Demo
    Felix Http server Demo
    osgi学习
    windows一个目录下最大文件数目
    oracle默认配置ora文件位置
    iptables配置(/etc/sysconfig/iptables)
  • 原文地址:https://www.cnblogs.com/blueberry006/p/8735268.html
Copyright © 2011-2022 走看看