SQL中的DDL语句

SQL语句分类

  • DDL(Data Definition Language):数据定义语言,用来定义数据库对象:库、表、列等。
  • DML(Data Manipulation Language):数据操作语言,用来定义数据库记录(数据)增删改。
  • DCL(Data Control Language):数据控制语言,用来定义访问权限和安全级别。
  • DQL(Data Query Language):数据查询语言,用来查询记录(数据)查询。

DDL操作数据库

创建数据库

CREATE DATABASE 语句用于创建新的数据库:
编码方式:gb2312,utf-8,gbk,iso-8859-1

语句格式:
//create database 数据库名
CREATE DATABASE mydb1;
//create database 数据库名 character set 编码方式
CREATE DATABASE mydb2 character SET GBK;  
//create database 数据库名 set 编码方式 collate 排序规则
CREATE DATABASE mydb3 character SET GBK COLLATE gbk_chinese_ci;

查看数据库

查看当前数据库服务器中的所有数据库

show databases;

查看前面创建的mydb2数据库的定义信息:

//show create database 数据库名;
Show CREATE DATABASE mydb2;

修改数据库

语句格式:
alter database 数据库名 character set 编码方式

查看服务器中的数据库,并把mydb2的字符集修改为utf8:
ALTER DATABASE mydb2 character SET utf8;

删除数据库

drop database 数据库名;

其他语句

查看当前使用的数据库:
select database;

切换数据库:       
use 数据库名;
use mydb2;

DDL操作表

CREATE TABLE 语句用于创建新表。

CREATE TABLE 表名(
列名1 数据类型 [约束],
列名2 数据类型 [约束],
列名n 数据类型 [约束]
);

说明: 表名,列名是自定义,多列之间使用逗号间隔,最后一列的逗号不能写
[约束] 表示可有可无
示例:

CREATE TABLE Employees(
 id INT ,
 age INT ,
 first VARCHAR(255),
 last VARCHAR(255)
);

常用数据类型:
int:整型
double:浮点型,例如double(5,2)表示最多5位,其中必须有2位小数,即最大值为
999.99;默认支持四舍五入
char:固定长度字符串类型; char(10) 'aaa ’ 占10位
varchar:可变长度字符串类型; varchar(10) ‘aaa’ 占3位
text:字符串类型,比如小说信息;
blob:字节类型,保存文件信息(视频,音频,图片);
date:日期类型,格式为:yyyy-MM-dd;
time:时间类型,格式为:hh:mm:ss
timestamp:时间戳类型 yyyy-MM-dd hh:mm:ss 会自动赋值
datetime:日期时间类型 yyyy-MM-dd hh:mm:ss

DDL其他表操作

删除表:
drop table 表名;

DROP TABLE table_name;

显示当前数据库中的所有表:

show tables;

查看表的字段信息:

desc 表名;

增加列:
alter table 表名 add 新列名 新的数据类型
在上面员Employees表的基本上增加一个image列:

ALTER TABLE employee ADD image blob;

修改job列,使其长度为60。
alter table 表名 change 旧列名 新列名 新的数据类型

ALTER TABLE employee change job job varchar(60);
或:
ALTER TABLE employee MODIFY job varchar(60);

列名name修改为username:
ALTER TABLE user CHANGE name username varchar(100);

删除列:
alter table 表名 drop 列名;

删除image列,一次只能删一列:
ALTER TABLE  employee DROP image;

修改表名:
alter table 表名 rename 新表名;

表名改为user。
ALTER TABLE user RENAME users;

查看表格的创建细节:
show create table 表名;

SHOW CREATE TABLE users;

修改表的字符集为gbk:
alter table 表名 character set 编码方式

ALTER TABLE users CHARACTER SET gbk;
  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Latest version 7.3.0.651 Released 10/31/2016 7.3.0.651 - 31st October SQL Prompt can now retrieve suggestions using Azure Active Directory password authentication (UserVoice) Formatting off comment tag now applies to itself (forum post) Fix for text being moved inside formatting off comment tags (Forum post) Support ticket 76423: ambiguous columns are now always qualified with their table/alias inside an ORDER BY clause 7.3.0.642 - 27th October Support for user account licensing (More info) 7.3.0.639 - 26th October Fix for insert semicolons error being displayed when trying to format a script in older versions of SSMS2016 (UserVoice and Forum post) Place first item on new line now works for ORDER BY in windowed functions (Forum post) 7.3.0.632 - 24th October Format action added for applying column alias style (UserVoice) Option added to change CREATE/ALTER parentheses separately from the global parentheses options Option added to insert an empty line between JOIN clauses Ole db provider names are now suggested for OPENROWSET and OPENQUERY Fix for incorrectly qualifying a column using the xml "modify" method (Forum post) Fix for syntax error being introduced when wrapping long lines containing multi-line strings 7.3.0.619 - 17th October Fix for script not being formatted if there are errors outside of the selected text (UserVoice) Whitespace at the end of the file is now maintained if "Preserve existing new lines between statements" is checked (UserVoice) Statements inside TRY/CATCH blocks are now formatted the same as BEGIN/END blocks Fix for parentheses alignment inside CASE expression (Forum post) 7.3.0.610 - 11th October Experimental feature added to refresh the SSMS IntelliSense (UserVoice) Fix for IN parentheses not being aligned correctly (Forum post) Fix for parentheses in VALUES clause not being aligned (Forum post) Renaming the file that a style is stored in will now rename the style Support ticket 74368: Fix for a semicolon being incorrectly added on an ALTER table statement with a masked column 7.3.0.598 - 6th October Formatting now indents nested JOIN clauses correctly Fix for formatting issue where line breaks were removed incorrectly after multi-line comments (Forum post) EXEC statements with a return value are now formatted Fix for formatting issue where whitespace was added after aliases (Forum post) Improved formatting for CREATE/ALTER with temporal tables Improved suggestions for ALTER with temporal tables Fix for issue with insert semicolons and temporal tables 7.3.0.587 - 3rd October Tables used in JOIN clauses are now treated as subsequent items (Forum post) 7.3.0.585 - 30th September Added new formatting options for JOINs Fix for formatting failing when right aligning AS keyword in CTEs Formatting no longer fails with some Azure Data Warehouse -specific statements Fix formatting for negative numbers in CASE expressions (Forum post) Fix for formatting failling with SELECT into variable with += (Forum post) 7.3.0.564 - 22nd September Fix for formatting failing on some scripts (Forum post) Option for adding spaces around parentheses contents is now used in more statements 7.3.0.564 - 20th September Support for formatting scripts containing SQLCMD mode and "GO 10" syntax New format action for adding/removing AS keyword for table/view alias definitions Simplified the parentheses "Place on new line" options ALTER DATABASE statements now respect the "Indent DDL clauses" option (Forum post) Column names in temp tables using quoted identifiers are now suggested Inline EXEC now correctly handles parameters using the DEFAULT value Fix for incorrect indentation when using UNION Fix for missing space in ORDER BY between function call and DESC keyword Fix for extra space being inserted between "<=" Fix for extra space being inserted after minus inside IN clause (Forum post) Support ticket 73192: Fix for Inline EXEC not inlining negative variables correctly Support ticket 73072: Fix for cancel button not responding when using "Open in Excel"

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值