码迷,mamicode.com
首页 > 数据库 > 详细

SQL语句之DDL

时间:2016-05-25 07:08:31      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

DDL:Databse Definition Language
建数据库
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [create_specification]

create_specification:
    [DEFAULT] CHARACTER SET [=] charset_name
    [DEFAULT] COLLATE [=] collation_name
example1:
查看字符集gbk,和gbk支持的排序规则
mysql> show character set like ‘gbk‘;
+---------+------------------------+-------------------+--------+
| Charset | Description            | Default collation | Maxlen |
+---------+------------------------+-------------------+--------+
| gbk     | GBK Simplified Chinese | gbk_chinese_ci    |      2 |
+---------+------------------------+-------------------+--------+
mysql> show collation like ‘gbk%‘;
+----------------+---------+----+---------+----------+---------+
| Collation      | Charset | Id | Default | Compiled | Sortlen |
+----------------+---------+----+---------+----------+---------+
| gbk_chinese_ci | gbk     | 28 | Yes     | Yes      |       1 |
| gbk_bin        | gbk     | 87 |         | Yes      |       1 |
+----------------+---------+----+---------+----------+---------+
mysql> create database if not exists students default character set = ‘gbk‘ default collate = ‘gbk_chinese_ci‘;
Query OK, 1 row affected (0.00 sec)
# cat db.opt 默认字符集和排序规则
default-character-set=gbk
default-collation=gbk_chinese_ci

怎样查看数据库的属性

修改数据库
ALTER {DATABASE | SCHEMA} [db_name] alter_specification
ALTER {DATABASE | SCHEMA} db_name UPGRADE DATA DIRECTORY NAME 升级数据库

alter_specification:
    [DEFAULT] CHARACTER SET [=] charset_name
  | [DEFAULT] COLLATE [=] collation_name

删除数据库
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
数据库重命名
修改数据库文件下的目录名称,或者新建目录后复制数据,重启mysql服务器少用

创建表的方式
查看表的属性
mysql> show table status like ‘lesson‘\G
1、直接定义一张空表;
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name(create_definition,...) [table_options] [partition_options]

example2:
CREATE TABLE tb1 (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, Name CHAR(20) NOT NULL, Age TINYINT NOT NULL)  ENGINE [=] engine_name
CREATE TABLE tb2 (id INT UNSIGNED NOT NULL AUTO_INCREMENT, Name CHAR(20) NOT NULL, Age TINYINT NOT NULL, PRIMARY KEY(id),UNIQUE KEY(name),INDEX(age))

mysql> create table course(course_id tinyint unsigned not null primary key,course_name char(20) not null) engine=MyISAM;
mysqlcreate table lesson(course_id tinyint unsigned not null auto_increment primary key,course_name char(20) not null) engine=MyISAM; auto_increment位置
mysql> insert into lesson(course_name) value (‘English‘),(‘Maths‘),(‘Music‘),(‘Physics‘),(‘Chemical‘); 填入内容
mysql> select * from lesson; 查看表内容
mysql> show indexes from course\G 查看索引

2、从其它表中查询出数据,并以之创建新表;字段属性可能会改变
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name[(create_definition,...)] [table_options] [partition_options] select_statement
example3:
mysql> create table lesson_1 select * from lesson where course_id <= 3;

3、以其它表为模板创建一个空表字段属性保持不变
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name { LIKE old_tbl_name | (LIKE old_tbl_name) }
example3:
mysql> create table test like course;

键也称作约束,可用作索引,属于特殊索引(有特殊限定):B+Tree
如何查看表的字段? desc 表名
table_option:
    ENGINE [=] engine_name 
    AUTO_INCREMENT [=] value |
    CHECKSUM [=] {0 | 1} |
    DELAY_KEY_WRITE [=] {0 | 1} |
    MAX_ROWS [=] value |
    [DEFAULT] CHARACTER SET [=] charset_name |
    [DEFAULT] COLLATE [=] collation_name

单字段:
    PRIMARY KEY
    UNIQUE KEY

单或多字段:
    PRAMARY KEY (col,...)
    UNIQUE KEY (col,...)
    INDEX (col,...)


修改表定义
ALTER TABLE
添加、删除、修改字段
添加、删除、修改索引
改表名
修改表属性
ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name [alter_specification [, alter_specification] ...] [partition_options]

alter_specification:
table_options
  | ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name ]
  | ADD [COLUMN] (col_name column_definition,...)
example5:
mysql> alter table lesson add starttime date default ‘2016-06-07‘;

  | ADD {INDEX|KEY} [index_name] [index_type] (index_col_name,...) [index_option] ...
  | ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...) [index_option] ...
  | ADD [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY] [index_name] [index_type] (index_col_name,...) [index_option] ...
example5:
mysql> alter table test1 add unique key (course_name);

  | ADD FULLTEXT [INDEX|KEY] [index_name] (index_col_name,...) [index_option] ...
  | ADD [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name,...) reference_definition
  | CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST|AFTER col_name]
example6:注意使用字段定义
mysql> alter table test1 change course_name lesson_name char(20) not null;

  | MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name]
  | DROP [COLUMN] col_name
  | DROP PRIMARY KEY
  | DROP {INDEX|KEY} index_name
如何删除unique ?
  | RENAME [TO|AS] new_tbl_name
example7:
mysql> alter table test1 rename to test;
mysql> rename table test to test_1;

  | ORDER BY col_name [, col_name] ...
  | CONVERT TO CHARACTER SET charset_name [COLLATE collation_name]
  | [DEFAULT] CHARACTER SET [=] charset_name [COLLATE [=] collation_name

example
mysql> create table students(student_id tinyint unsigned not null auto_increment primary key,name char(20) not null,course_id tinyint not null);
mysql> insert into students (name,course_id) value (‘Yang‘,2),(‘Zhang‘,3),(‘Wang‘,1);
mysql> select name,course_name from lesson,students where lesson.course_id=students.course_id; 
+-------+-------------+
| name  | course_name |
+-------+-------------+
| Yang  | Maths       |
| Zhang | Music       |
| Wang  | English     |
+-------+-------------+


删除表
DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ... [RESTRICT | CASCADE]级联,危险

InnoDB支持外键
mysql> alter table students add foreign key foreign_cid (course_id) references lesson (course_id);
ERROR 1005 (HY000): Can‘t create table ‘students.#sql-500b_8‘ (errno: 150)
mysql> alter table lesson engine=InnoDB;


索引创建:
CREATE [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name ON tbl_name (index_col_name [(length)] [ASC | DESC],...) 【升序|降序】
删除索引:
DROP [ONLINE|OFFLINE] INDEX index_name ON tbl_name
DROP INDEX `PRIMARY` ON t;
example
mysql> create index index_on_name on students (name);
mysql> drop index index_on_name on students;
mysql> create index index_on_name on students (name (5) desc);

查看索引
SHOW INDEXES FROM tb_name:显示指定表上的索引




SQL语句之DDL

标签:

原文地址:http://hiyang.blog.51cto.com/10728919/1782722

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!