DDL

来源:互联网 发布:网络正常微信连接不上 编辑:程序博客网 时间:2024/05/22 02:09

DDL data define language

create    table / index / view drop       table alter     table add column /modify column /drop columndesc
# 加外键,orderbooklist(order_no)-> orderlist(order_no)alter table orderbooklist add foreign key (order_no) references orderlist(order_no);

表之间的关系

一对一 、一对多、多对一、多对多

关联关系写在多的一方

如果是多对多则新建关系表

DDL

/*用旧表创建一个新表*/create table new_stu like stu;desc new_stu;/*从旧表中选一些字段组成新表*/create table stu_2 as select id,name,height from stu;desc stu_2;/*创建索引*/create index stu_dex on stu(name);select*from stu where stu.name='x';/*删除索引*/drop index stu_dex on stu;CREATE TABLE `clazz` (    `id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '主键',    `name` VARCHAR(50) NOT NULL DEFAULT '0',    `stu_count` INT(11) NOT NULL DEFAULT '0',    PRIMARY KEY (`id`));CREATE TABLE `sstu` (    `id` INT(11) NOT NULL AUTO_INCREMENT COMMENT '主键',    `name` VARCHAR(25) NOT NULL COMMENT '姓名',    `class_id` INT(11) NOT NULL,    `gender` CHAR(1) NOT NULL COMMENT '性别',    `birthday` DATETIME NOT NULL COMMENT '出生年月',    `height` DECIMAL(5,2) NOT NULL COMMENT '身高',    PRIMARY KEY (`id`));/*创建视图*/create view stu_view as select  sstu.name as sstu_name,clazz.name as class_namefrom sstu,clazzwhere sstu.class_id=clazz.id;drop view stu_view;###########create database sms;use sms;create table student(         stu_no varchar(20) not null primary key,         sto_name varchar(50) not null);information_schemainformation_schemadesc student;/*描述表结构*/studentdrop table student;#创建表create table students(  id int not null  auto_increment/*自动加*/ primary key comment '主键',  name varchar(50) not null comment '姓名',  sex char(1) not null comment '性别',  birthday datetime not null comment '生日',  height decimal(5,3) not null comment '身高' );#添加列alter table students add column vision decimal(2,1) not null comment '视力';desc students;#删除列alter table students drop column vision;desc students;#修改列alter table students modify column sex char(2) not null comment '性别';desc students;alter table students change column sex gender char(2) not null comment '性别';desc students;#重命名表rename table students to stu;desc stu;/*创建表*/create table shop(  id int not null auto_increment primary key comment '主键',  name varchar(20) not null  comment '名字',  price decimal(10,2) not null comment '价格');#添加列alter table shop add column shopdate datetime not null comment '日期';desc shop;#删除列alter table shop drop column price;desc shop;#修改列alter table shop change column name  shopname varchar(50) not null comment '名字';desc shop;

常用数据类型:

数值型:

类型 大小 范围(有符号) 范围(无符号) 用途 TINYINT 1 字节 (-128,127) (0,255) 小整数值 SMALLINT 2 字节 (-32 768,32 767) (0,65 535) 大整数值 MEDIUMINT 3 字节 (-8 388 608,8 388 607) (0,16 777 215) 大整数值 INT或INTEGER 4 字节 (-2 147 483 648,2 147 483 647) (0,4 294 967 295) 大整数值 BIGINT 8 字节 (-9 233 372 036 854 775 808,9 223 372 036 854 775 807) (0,18 446 744 073 709 551 615) 极大整数值 FLOAT 4 字节 (-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) 0,(1.175 494 351 E-38,3.402 823 466 E+38) 单精度浮点数值 DOUBLE 8 字节 (-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 对DECIMAL(M,D) ,m表示总位数,d表示小数点后的位数,如果M>D,为M+2否则为D+2 依赖于M和D的值 依赖于M和D的值 小数值

日期型

类型 大小(字节) 范围 格式 用途 DATE 3 1000-01-01/9999-12-31 YYYY-MM-DD 日期值 TIME 3 ‘-838:59:59’/’838:59:59’ HH:MM:SS 时间值或持续时间 YEAR 1 1901/2155 YYYY 年份值 DATETIME 8 1000-01-01 00:00:00/9999-12-31 23:59:59 YYYY-MM-DD HH:MM:SS 混合日期和时间值 TIMESTAMP 4 1970-01-01 00:00:00/2037 年某时 YYYYMMDD HHMMSS 混合日期和时间值,时间戳

字符串型

类型 大小 用途 CHAR 0-255字节 定长字符串 VARCHAR 0-65535 字节 变长字符串 TINYBLOB 0-255字节 不超过 255 个字符的二进制字符串 TINYTEXT 0-255字节 短文本字符串 BLOB 0-65 535字节 二进制形式的长文本数据 TEXT 0-65 535字节 长文本数据 MEDIUMBLOB 0-16 777 215字节 二进制形式的中等长度文本数据 MEDIUMTEXT 0-16 777 215字节 中等长度文本数据 LONGBLOB 0-4 294 967 295字节 二进制形式的极大文本数据 LONGTEXT 0-4 294 967 295字节 极大文本数据