MYSQL review the basics of 1

Database (the data warehouse is stored)

1. The storage amount is divided up and safety:

  • Large databases: DB2 Oracle (graduation) Hbase Bank Public Security Bureau (did not work overtime Network) mobile
  • Medium-sized databases: mysql sqlserver (.net) mogodb (mysql: Ali used)
  • Small databases: access the school network

    Medium-sized databases: mysql
    Uninstall

    1. In Control Panel unloading mysql
    2. Remove the mysql Registrar's information on: win + R regedit

    under File delete c disc Program / mysql
    4. Win7
    win10

    installation





Common command mysql

- Create a database
CREATE DATABASE database name;
- delete the database
DROP DATABASE database name;
- switching database
use the database name;
- create table
create table table name (
field name data types,
field names data types,
...
);
type of data:

  • String: chr / varchar
  • It represents an integer: int / bigint
  • Decimal: float / double
  • Date: date
  • Date Time: datetime
    cases:
create table t_user(
         userid int,
         username varchar(20),-- 20 最高可以表示20个字符
         password varchar(20),
         birth date
 )

- drop table
drop the Table t_user;
- modify table structure

  • Increasing one: alter table table add column Column Name Data Type;
  • Delete one: alter table table name drop column column name;
  • 修改列的数据类型:alter table 表名 modify 列名 新的数据类型;
    练习:创建一个商品表
  • 字段:商品编号、商品名称varchar(3)、商品价格、商品生产日期。
  • 增加一个字段:保质期。
  • 把商品名称varchar(3)改成varchar(50)。
create table product(
    p_id int,-- 设置表的商品编号列,数据类型为int
    p_name varchar(3),-- 设置表的商品名称列,数据类型为varchar
    p_price float,-- 设置表的商品价格列,数据类型为float
    p_birthday datetime
);
alter table product add column p_keep int;
alter table product modify p_name varchar(50);
  • 创建表的约束:限制该字段的内容
    • 主键约束(primary):标识每一条记录的。 特点:唯一、非空、一张表中只能有一个主键
      • 表创建好了怎么添加主键约束
        alter table 表名 add contraint 约束名 primary key (列名);
      • 建表的时候添加
        列名 数据类型 primary key,
        primary key(列名1,列名2),-- 联合主键
    • 唯一约束(unique):内容唯一
      • 表创建好:
        alter table 表名 add contraint 约束名 unique (列名);
      • 建表的时候添加:
        列名 数据类型 unique,
    • 非空约束(not null):
      • 建表的时候添加:
        列名 数据类型 not null,
    • 默认约束(default):如果该列没有添加内容那么该列采用默认值
      • 建表的时候添加:
        列名 数据类型 default 默认值,
    • 检查约束(check):但是mysql不支持该约束。
      • 建表的时候添加:
        列名 数据类型 check (条件),
    • 外键约束(foreign):
      1.在一张表中一个字段的值引用另一张表中主键的值。一对多在多的一方添加外键,建好所有表结构以后添加
      alter table 表名 add constraint 约束名 foreign key (列名) references 表名(列名);
      练习:
      1519c87129c9b78fc12b02629e0c4a61.png
CREATE DATABASE Company;
use Company;
create table Department(
    dept_id int primary KEY,
    dept_name char(10) not null
);
create table Emp(
    e_id int primary key,
    e_name char(10) not null,
    e_sex char(2) default '男' check('男' or '女') ,
    e_birthday datetime,
    party char(2) check('是' or '否'),
    e_jointime datetime,
    dept int,
    np char(10)
);
create table Salary(
    emp_id int,
    mon int check(mon between 1 and 12),
    base_salary double check(base_salary >= 0),
    allowance double check(allowance >= 0),
    subsidy double check(subsidy >= 0),
    bonus double CHECK(bonus >= 0),
    deduct double check(deduct >= 0),
    revenue double check(revenue >= 0)
);
alter table Emp add constraint aa foreign key (dept) references Department(dept_id);
alter table Salary add constraint ab foreign key (emp_id) references Emp(e_id);
往表中插入数据

1.插入全部字段的数据
insert into 表名 values(值1,值2,值3...);-- values后面的值个数一定要和表字段的个数匹配
2.插入部分字段的数据
insert into 表名(字段,字段...) values(值1,值2...);--

删除表中的数据

1.删除所有的数据
delete from 表名;
2.根据条件删除满足条件的记录
delete from 表名 where 条件;
条件:
关系运算符:>,<,>=,<=,=,!=
逻辑运算符:and(且) or(或)
其他:between 值1 and 值2 大于等于值1小于等于值2
in(值1,值2...)
is null
is not null
模糊: like 和 通配符 _ 表示通配一个字符 %表示多个字符

Guess you like

Origin www.cnblogs.com/wuliqqq/p/11229209.html