• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

小弟我的复习-Mysql DDL语句

mysql 搞代码 7年前 (2018-06-03) 158次浏览 已收录 0个评论

我的复习–mysql DDL语句
Mysql的学习笔记
1 常用命令
查看mysql上有多少个数据库  show databases;

使用数据库 use 数据库名;
显示数据库内的表 show tables;

创建数据库 create database 数据库名;
查看表结构  desc表名

删除数据库
drop database 数据库名

数据库的DDL –》操作数据库对象的语句,包括创建create 删除drop 修改 alter 数据库对象

创建表
普通方法
create table test(test_id int,
test_price decimal,
test_name varchar(255) default ‘xxx’,
test_desc text,
test_img blob,
test_date datetime);
  通过自查询的方法
create table test2 as select * from test;
修改表
   添加表字段
alter table test2 add(age int);
   修改字段的类型
我们先添加一列 haha_id
alter table test2 add haha_id varchar(255);
现在在修改haha_id的类型
alter table test2 modify haha_id int;
   删除列
alter table test2 drop haha_id;
   删除表的语法
drop table 表名
drop table test2;
   删除整个表的记录 truncate 表名
truncate test2;   这个是delete的豪华升级版直接就删除了整个表的数据但是对表的结构还是

已于保留。

建立约束
create primary_test(
test_id int primary key,
test_name varchar(255)

);
建表示创建表级别的主键约束
create primary_test2(

test_id int not null,
test_name varchar(255),
test_pass varchar(255),
constraint test2_pk primary key(test_id)
);
创建表时 建多列组合的主键约束
create table primary_test3(
test_id int not null,
test_name varchar(255),
test_pass varchar(255),
constraint test3_pk primary key(test_id,test_pass)
);
删除主键约束
alter table primary_test3 drop primary key;
指定表添加主键约束
alter table primary_test4 add primary key(test_id);
设置主键自增涨
create table primary_test4(
test_id int auto_increment primary key,
test_name varchar(255)
);
创建外键约束
教师—学生表 看成简单的一对多的关系
create table teacher_table(
teacher_id int auto_increment primary key,
teacher_name varchar(255)
);
create table student_table(
student_id int auto_increment primary key,
stidemt_name varchar(255),
teacher_id int,
foreign key(teacher_id) references teacher_table(teacher_id)
);[/b][/size][/size]


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:小弟我的复习-Mysql DDL语句

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址