您的位置:首页 > 数据库 > MySQL

mysql--数据定义语言DDL

2017-07-19 12:35 309 查看
DDL对数据库的操作命令:

(1)数据库的创建:

create database + 数据库名;
(2)删除数据库:
drop database + 数据库名;
(3)使用该数据库:
use + 数据库名;
(4)显示所有数据库
show databases;


DDL数据库表格的操作命令:

1.create创建表:

create table 名字(
列名 类型,
列名 类型,
列名 类型
);
CREATE TABLE user (
User char(16) COLLATE utf8_bin NOT NULL DEFAULT '',
Select_priv enum('N','Y') CHARACTER SET utf8 NOT NULL DEFAULT 'N',
ssl_cipher blob NOT NULL,
max_user_connections int(11) unsigned NOT NULL DEFAULT '0',
authentication_string text COLLATE utf8_bin,
PRIMARY KEY ('Host','User')
) ENGINE=MyISAM  DEFAULT CHARSET=utf8  COMMENT='Users and global privileges';


列属性的约束:

1.  自增  auto_increment

2.  主键(复合主键)primary key 默认为非空唯一primary key(stuid,carid)

3.  外键  foreign key  

constraint 约束名 foreign key (外键列名)references 被参考表名(被参考列)

constraint fk_t1  foreign key(fid)references t1(uid)

alter table tte add constraint ft foreign key(uid) references t3(id)

4.   索引  index

5.   注释  comment

6.   Default  

7.   是否为空 null not null

8.   唯一 unique

9.   检查check

age  int check(age  between  18  and  25),

注意:先有主键,后有外键。主外键属性保持一致,主外键名不能重复。

自增和默认不能同时使用

列级约束 null notnull

表级约束 primarykey froreign key unique check

2.drop删除表
drop table + 表名;


3.alter

(1)增加一列
alter table +表名 add  列名 类型;
(2)修改一列
alter  table  表名  modify  列名  修改后的类型;


(3)删除一列
alter table 表名 drop 列名;


(4)为表重命名
alter  table  表名  rename  to  新表名


(5)为列重命名
alter  table  表名  change  原列名 新列名


(6)修改整个表的字符集
alter table 表名 charset=gbk;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: