zoukankan      html  css  js  c++  java
  • MySQL数据库基本使用(DDL)

    MySQL是一种开源的关系型数据库管理系统,并且因为其性能、可靠性和适应性而备受关注。下面是最近一个月MySQL、Oracle、SQL Server的百度指数搜索指数对比:

    可以看到,在最近一个月,MySQL的搜索量要远远高于其他两种数据库语言,表明了MySQL的用户多、受关注度高。在平时的项目中也经常使用,因此,先整理一下最基本的库和表结构的增删改查,也就是DDL(数据定义语句),其他内容后续再整理。

    CREATE DATABASE db1 CHARSET utf8; --创建数据库db1
    show DATABASES; --查看所有数据库
    show create database db1; --查看新创建的数据库db1
    drop database db1; --删除数据库db1
    use db1; --选择数据库db1
    show tables; --查看数据库中的所有表
    desc t1; --查看数据表t1的结构
    --创建数据表t1
    create table t1(
    id int PRIMARY key auto_increment,
    name varchar(11) not null,
    age int(3),
    sex enum("male","female")); --使用枚举类型,sex的值只能是枚举中的任意一个
    select * from t1; --无条件查询表t1的所有记录
    --往表t1中插入数据
    insert into t1(id,name,age,sex) values(1,"wang",22,"male");
    insert into t1(id,name,age,sex) values(2,"zhang",23,"female"),
    (3,"li",22,"female"),
    (4,"chen",22,"male");
    insert into t1(name,age,sex) values("zhao",22,"male");
    describe t1; --查看表结构
    alter table t1 rename t2; --修改表名
    desc t2;
    alter table t2 engine=innodb; --修改表的存储引擎
    --增加字段
    alter table t2 
    add address varchar(255) not null after name;
    alter table t2 
    add status varchar(255) not null first;
    --删除字段
    alter table t2 drop status;
    --修改字段
    alter table t2 modify address varchar(125) not null;
    alter table t2 change address dizhi varchar(22) not null;
    alter table t2 change dizhi address varchar(255) not null;
    --删除主键
    alter table t2 modify id int(15) not null;
    desc t2;
    --复制表
    create table t3 select * from t2 where 1=2; --只复制表结构,不复制记录,key也不复制
    show tables;
    desc t3;
    create table t4 select * from t2; --复制表结构和记录,key不复制
    alter table t4 add primary key(id,name);
    alter table t4 modify id int auto_increment;
    desc t4;
    --删除表
    drop table t3;
    alter table t3 add money decimal(4,2) not null;
    alter table t3 modify id int primary key auto_increment not null;
    insert into t3(name,address,age,sex,money) values("zhang","pingling",22,"male",99.99); 
    --decimal(m,n) m表示数据的总位数,n表示小数点后的位数,不算负号,精确的
    --sql优化:创建表时,定长的类型往前放(比如性别),变长的类型网后放(比如地址、描述信息)
  • 相关阅读:
    Fire and Motion[转载]
    HLSL2GLSL v0.9 Released
    CEGUI Release of 0.5.0 stable by CrazyEddie 6th November 2006
    MapInfo 连接Oracle
    MapInfo连接SQLServer
    视图的创建及使用(sql server 2005)
    MapInfo 建立永久表
    MapInfo Update Feature
    MapInfo导入.TAB和.mws的方法
    触发器的创建及使用(sqlserver 2000)
  • 原文地址:https://www.cnblogs.com/andrew3/p/12650117.html
Copyright © 2011-2022 走看看