聊聊数据库~SQL基础篇 (3)

详情可以查看我之前写的文章:https://www.cnblogs.com/dotnetcrazy/p/10374091.html

3.1.1.创建、删除数据库 -- 如果存在就删除数据库 drop database if exists dotnetcrazy; -- 创建数据库 create database if not exists dotnetcrazy; 3.1.2.创建、删除表 -- 如果存在就删除表 drop table if exists dotnetcrazy.users; -- mysql> help create table(低版本的默认值不支持函数) -- 创建表 create table users(字段名 类型 修饰符,...) create table if not exists dotnetcrazy.users ( id int unsigned auto_increment, -- 主键,自增长【获取ID:last_insert_id()】 username varchar(20) not null, password char(40) not null, -- sha1:40 email varchar(50) not null, ucode char(36) not null,-- default uuid(), -- uuid createtime datetime not null,-- default now(), updatetime datetime not null,-- default now(), datastatus tinyint not null default 0, -- 默认值为0 primary key (id), -- 主键可多列 unique uq_users_email (email), index ix_users_createtime_updatetime (createtime, updatetime) -- 索引,不指定名字默认就是字段名 ) -- 表选项 -- engine = 'innodb', -- 引擎 -- character set utf8, -- 字符集 -- collate utf8_general_ci, -- 排序规则 ; 3.1.3.修改表 -- 修改表 mysql> help alter table -- 3.1.添加一列 alter table tb_name add 列名 数据类型 修饰符 [first | after 列名] alter table dotnetcrazy.users add uid bigint not null unique first; -- MSSQL没有[first | after 列名] -- 在email后面添加手机号码列 -- 手机号不会用来做数学运算,varchar可以模糊查询(eg:like ‘138%’) -- 牵扯到国家代号时,可能出现+、-、()等字符,eg:+86 alter table dotnetcrazy.users add tel varchar(20) not null after email; -- 3.2.删除一列 alter table tb_name drop 字段名 alter table dotnetcrazy.users drop uid; -- 3.3.添加索引 alter table tb_name add index [ix_name] (列名,...) alter table dotnetcrazy.users add index ix_users_ucode (ucode); -- 不指定名字默认就是字段名 -- add index (ucode, tel); -- 不指定索引名字,默认就是第一个字段名 -- 添加唯一键 alter table tb_name add unique [uq_name] (列名,列名2...) alter table dotnetcrazy.users add unique uq_users_tel_ucode (tel, ucode); -- add unique (tel, ucode);-- 不指定索引名字,默认就是第一个字段名 -- 3.4.删除索引 alter table tb_name drop index ix_name alter table dotnetcrazy.users drop index ix_users_ucode; -- 删除索引(唯一键) alter table tb_name drop index uq_name alter table dotnetcrazy.users drop index uq_users_tel_ucode; -- drop index tel; -- 唯一键的索引名就是第一个列名 -- 3.5.修改字段 -- 1.修改字段名:`alter table tb_name change 旧列名 新列名 类型 类型修饰符` -- 此时一定要重新指定该列的类型和修饰符 alter table dotnetcrazy.users change ucode usercode char(36); -- default uuid(); -- 2.修改字段类型 alter table dotnetcrazy.users modify username varchar(25) not null; -- 3.添加默认值:`alter table tb_name alter 列名 set default df_value` alter table dotnetcrazy.users alter password set default '7c4a8d09ca3762af61e59520943dc26494f8941b'; 3.2.SQLServer

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wspwyg.html