数据库语言:DDL,DML,DCL,DQL(结合数据库实例分析)

1.DDL(Data Definition Languages)数据库定义语言,主要包括库和表的修改。

(1)操作数据库

 1)创建数据库(create database student;)

 2)查看所有数据库(show databases;)

 

 3)查看某个数据库的定义信息(show create database student; )

 4)修改数据库字符信息(alter database student character set utf8;)

5)使用数据库(use student;)

 6)删除数据库(drop database student;)

(2)操作数据库表

1)创建表

create table computer(
  id  int not null,
  name char(20),
  age  int,
  primary key (id,name)
 )engine=innoDB AUTO_INCREMENT=1 default charset=utf8;

create table college(
  id int not null primary key,
classid int not null,
name char(20)
)engine=innoDB AUTO_INCREMENT=1 default charset=utf8;


-- [engine参数用来设置引擎类型,常用的有innoDB和myISAM引擎]
   DEFAULT CHARSET=utf8 数据库默认编码为utf-8
   AUTO_INCREMENT=1 自增键的起始序号为1
-- 外键中的级联关系:
	[ON DELETE RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT]
	[ON UPDATE RESTRICT | CASCADE | SET NULL | NO ACTION | SET DEFAULT]
	RESTRICT:限制外表中的外键改动,默认值
    CASCADE:跟随外键改动
 	SET NULL:设空值
	SET DEFAULT:设默认值
	NO ACTION:无动作,默认的

 

2) 复制表

-- 只复制表结构及约束,但不复制数据
create table music like computer;
-- 复制表结构及数据,但不复制约束
create table math as select * from computer;

3)添加表的一列

alter table computer add column hobby char (20) not null after age;

 4)添加主键和唯一标识

alter table computer add PRIMARY KEY(列名);
alter table computer add UNIQUE KEY(列名);

5)添加外键约束

-- 自己指定外键的名字(删除外键需要外键的名字)
alter table 表名 add constraint 外键名 foreign key(设为外键的列名) REFERENCES 外表表名(对应的表的主键列名);
alter table computer add constraint stu_1 foreign key(id) references college(id);

-- 系统自动生成一个外键的名字(用'show create table 表名'查看外键名字)
alter table 需要添加外键表名 add FOREIGN KEY 需要添加外键表名(设为外键的列名) REFERENCES 被添加外键表名(设为被添加外键的列名);
alter table computer add foreign key computer(id) references college(id);

6)查看表结构(desc computer;)

7)查看创建表的SQL语句(show create table computer;)

8)删除表(drop table computer),不删了。

9)删除表的一列(alter table computer drop foreign key 外键名;)

10)删除索引(alter table 表名 drop index 索引的列名;)

11)修改表名

alter table 表名 rename [to] 新表名;
-- 注:[]里的内容为可加可不加,或根据需要加
rename table 表名 to 新表名;

 12)修改表的列名的类型(不可修改列名)

alter table 表名 modify [column] 列名 列类型 [约束] [first|after 某列名];
-- 例:alter table teacher modify name varchar(10);
alter table 表名 change [column] 列名 列名 列类型 [约束] [first|after 某列名];
-- 例:alter table teacher change name name varchar(11) first;

 13)修改表的列名(可修改列名)

alter table 表名 change [column] 原列名 新列名 列类型 [约束] [first|after 某列名];
-- 例:alter table teacher change id teacher_id varchar(11);

2.DML(Data Manipulation Language)数据操控语言,对数据库表数据的操作。

 

3.DCL(Data Control Language)数据控制语言,控制数据库的访问级别。

4.DQL(Data Query Language)数据查询语言,查询数据库内容。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值