zoukankan      html  css  js  c++  java
  • mysql的相关操作

    安装mysql

    安装windows服务

    破解密码

    统一字符编码

    基本的sql语句

    - 数据库
    - show databases
    - create database db1 charset gbk;
    - use db1;
    - drop database db1;
    - alter database db1 charset utf8
    - 表
    - a = 2 a='2' String a = '2';
    - create table t1(id int,name varchar(20) );
    - alter
    - drop table t1;
    - select id from t1; select id,name from t1; select * from t1;
    - desc t1;
    - 记录
    - insert into t1(id,name) values(1,'alex');
    - insert into t1 values();插入空的值
    - update
    - delete table t1 where id =2;

    今日内容

    补充内容:创建用户和授权

    1.创建用户:
    # 指定ip:192.118.1.1的mjj用户登录
    create user 'mjj'@'192.118.1.1' identified by '123';
    # 指定ip:192.118.1.开头的mjj用户登录
    create user 'mjj'@'192.118.1.%' identified by '123';
    # 指定任何ip的mjj用户登录
    create user 'mjj'@'%' identified by '123';

    2.删除用户
    drop user '用户名'@'IP地址';


    3.修改用户
    rename user '用户名'@'IP地址' to '新用户名'@'IP地址';

    4.修改密码
    set password for '用户名'@'IP地址'=Password('新密码');


    对当前的用户授权管理
    查看权限
    show grants for '用户'@'IP地址'

    #授权 mjj用户仅对db1.t1文件有查询、插入和更新的操作
    grant select ,insert,update on db1.t1 to "mjj"@'%';

    # 表示有所有的权限,除了grant这个命令,这个命令是root才有的。mjj用户对db1下的t1文件有任意操作
    grant all privileges on db1.t1 to "mjj"@'%';
    #mjj用户对db1数据库中的文件执行任何操作
    grant all privileges on db1.* to "mjj"@'%';
    #mjj用户对所有数据库中文件有任何操作
    grant all privileges on *.* to "mjj"@'%';

    远程连接:
    mysql -uskx -P3306 -h 192.168.15.113 -p123

    复制表

    #即复制表结构 又复制记录
    create table t2 select * from db1.t1;

    # 只复制表结构,不复制记录
    create table t2 select * from db1.t1 where 1>3;
    create table t2 like db1.t1;

    数据类型

    整型 默认是有符号

    数据类型 无符号(unsigned)和有符号 用0填充 zerofill

    约束的作用: 保证数据的完整性和一直性

    - tinyint [-128~127] 小整数
    - int 整数
    - bigint 极大整数

    create table t1(id int(4) unsigned,name char(20));

    ---

    浮点型

    - float 单精度 随着小数位数的增多,不准确
    - double 双精度 随着小数位数的增多.不准确,比float要准确
    - decimal 小数 精准的小数

    日期类型

    year 年份 (1901~2155)

    date 年月日

    time 时分秒

    datetime 年月日 时分秒

    now() sql语言中自带的内容函: 获取当前的时间(根据数据类型)

    create table t10(born_year year,intClass datetime);

    字符类型

    - char 定长,简单粗暴,浪费空间,存取速度快
    - varchar 变长,精准,节省空间,存取速度慢

    length():查看字节数
    char_length():查看字符数

    枚举和集合

    create table consumer(
    id int,
    name varchar(50),
    sex enum('male','female','other') default 'male',
    level enum('vip1','vip2','vip3','vip4'),#在指定范围内,多选一
    fav set('play','music','read','study') #在指定范围内,多选多
    );

    注意:在sql中使用tinyint(1)来表示boolean类型

    完整性约束

    not null 与 default

    - 如果单独设置not null 不能插入空值
    - 如果即设置了not null,又指定default,可以插入空值,会走default

    unique key

    单列唯一

    create table t4(
    id int not null,
    name char(20) unique
    );

    create table t4(
    id int not null,
    name char(20),
    unique(name)
    );
    insert into t4(id,name) values(1,'alex');
    insert into t4(id,name) values(1,'wusir');

    多列唯一

    - 只要有一列相同,不能插入

    create table t5(
    id int,
    name char(20),
    unique(id),
    unique(name)
    );

    联合唯一 ***

    - 多列相同时,不能插入

    create table t6(
    id int,
    name char(20),
    unique(id,name)
    );

    应用场景: 选课系统,一个学生可以选择多个课程,一个课程可以被多个学生选择,

    student_id course_name

    100 '生物'

    101 '生物'

    100 '化学

    primary key

    化学反应: not null + unique

    单列主键 不能为空 并且是唯一

    # primary key 索引(针对于大量数据) 查询速度要快
    create table t7(
    id int primary key,
    name varchar(10) unique
    );

    create table t8(
    id int not null unique,
    name varchar(10) unique
    );

    联合主键

    create table t9(
    id int,
    name varchar(10),
    primary key(id,name)
    );

    auto_increment

    create table student(
    id int primary key auto_increment,
    name varchar(20) not null,
    sex enum('male','female') default 'male',
    ip varchar(20) unique
    );

    insert into student(name,sex,ip) values ('alex','female','127.0.0.5'),('wusir','male','173.45.32.1');

    *清空表区分delete和truncate的区别:*

    delete from t1; #如果有自增id,新增的数据,仍然是以删除前的最后一样作为起始。

    truncate table t1;数据量大,删除速度比上一条快,且直接从零开始。

    foreign key ***

    外键

    # 先创建主表
    create table dep(
    id int primary key auto_increment,
    name char(10) unique,
    dep_desc varchar(50) not null
    );
    # 校区表
    create table school(
    id int primary key auto_increment,
    addr varchar not null
    )



    # 创建从表
    create table emp(
    eid int primary key auto_increment,
    name char(10) not null,
    age int not null,
    dep_id int,
    school_id int,
    constraint fk_dep foreign key(dep_id) references dep(id)
    on delete cascade
    on update cascade,
    constraint fk_school foreign key(school_id) references school(id)
    on delete cascade
    on update cascade,
    );

    insert into dep(name,dep_desc) values('校长部','校长管理有限部门'),('公关部','公关管理有限部门'),('IT部门','IT技术有限部门'),('财务部','管钱很多部门');
    insert into emp(name,age,dep_id)
    values
    ('alex',18,1),
    ('wusir',30,2),
    ('吴老板',20,3),
    ('马老板',18,4),
    ('邱老板',20,2),
    ('女神',16,3);

  • 相关阅读:
    read-uncommited 下脏读的实现
    MySQL 加锁处理分析【重点】
    Next-key locking是如何解决幻读(当前读)问题的
    spring ioc
    讨论 update A set number=number+ ? where id=?的原子性 (含数据库原理)
    PESSIMISTIC_READ & PESSIMISTIC_WRITE 与 共享锁 & 排它锁
    innodb当前读 与 快照读 and rr级别是否真正避免了幻读
    java finalize及实践
    uva 539 The Settlers of Catan
    zoj 1016 Parencodings
  • 原文地址:https://www.cnblogs.com/bigc008/p/9996653.html
Copyright © 2011-2022 走看看