My_Project

资料整理


SQL语句-DDL

<p>DDL : 数据定义语句 Data Definition Language 针对数据库和表:create、alter 、drop</p> <p>1、操作数据库</p> <pre><code>查看数据库 show databases; 使用数据库 use 数据库名; 例://使用数据库db; use db; 或 select * from db.mytable; 创建数据库 create database 数据库名; 例:创建一个名为students的数据库 create database student; 删除数据库 drop database 数据库名; 例:删除student这个数据库 drop database student; 修改数据库名称 方法1: rename database olddbname to newdbname 这个是5.1.7到5.1.23版本可以用,但是官方不推荐,会有丢失数据的危险 方法2: 1.创建新的数据库名称; 2.mysqldum导处要改名的数据库信息到新的数据库中; 3.删除原来的旧库 这种方法是安全的,但是如果数据量大,会很耗时 方法3: 假设将数据库名keru改为new_keru;MyISAM直接更改数据库目录下的文件即可: mysql -uroot -p123456 -e 'create database if not exists new_keru' list_table=$(mysql -uroot -p123456 -Nse "select table_name from information_schema.TABLES where TABLE_SCHEMA='keru'") for table in $list_table do mysql -uroot -p123456 -e "rename table keru.$table to new_keru.$table" done 这里用到了rename table更改表名的命令,如果新表名后面加数据库,将会将原来的数据库的表移动到新的数据库,这种方法既安全又快速。</code></pre> <p>2、操作表格</p> <pre><code>查看表结构 desc 表名; 查看数据库中存在的表 show tables; 创建表 create table 表名(字段1 字段类型,字段2 字段类型,字段3…); 例:创建一个student表,包含学生学号\姓名\年龄\性别信息 create table student(id int,name char(20),age int,sex char(10)); 删除表 drop table 表名 修改表名 alter table 原表名 rename 新表名; 例://把user1表名修改成user alter table user1 rename user;</code></pre> <p>3、修改表结构</p> <pre><code>增加字段 alter table 表名 add 字段名 字段类型 字段约束 练:给student表增加一个语文成绩chinese字段 增加一个字段在某个字段后 alter table 表名 add 字段名 字段类型 after 字段名; 增加一个字段在首行 alter table 表名 add 字段名 字段类型 first; 删除字段 alter table 表名 drop 字段名 练1:删除student表中的语文成绩chinese字段 练2:删除student表中的主键 alter table student drop primary key; 注:表中只有一个字段时,无法用drop来删除字段 删除主键 lter table 表名 drop primary key; 修改表结构 alter table 表名 change 旧字段名 新字段名 字段类型 字段约束 位置(first或after) 清除表中数据 truncate table 表名; delete from 表名; 注: 1、truncate table语句比delete from语句运行速度要快 2、truncate清空表的数据的性能要比delete要高 3、truncate不是使用where条件 复制表(备份) create table备份表的表名 as select * from 原表名; 例:将student表备份,备份表名为student_copy create table student_copy as select * from student; 还原表中数据 insert into 原表名 select * from 备份表的表名; 例:将备份表student_copy中的数据还原到原表student中 insert into student select * from student_copy</code></pre> <p>4、操作索引</p> <pre><code>创建索引 cerate index 删除索引 drop index 创建数据库用户 通过grant命令创建具有某种权限的用户。其中grant的常用用法如下: grant all on 数据库名 to NewUserName@HostName identified by 'password '; 例: //在test库中创建一个user2用户,用户密码为'123456' grant all on test to user2@localhost identified by '123456'; grant select,insert,update on 数据库名.表名字 to NewUserName@HostName identified by “password”; grant update,delete on school.student to user3@localhost identified by '123456'; 登录用户 mysql -h localhost -u 用户名 -p</code></pre> <p>5、数据类型</p> <pre><code>(1)数据类型 int 整数型;4字节;整数值 float 浮点型;4字节;单精度,浮点数值(小数) double 浮点型双精度型;8字节;双精度,浮点数值,比float精度更大的小数 bit(M) 位类型,M指定位数,默认值1,范围1-64 tinyint[unsigned] 带符号的范围是-128到127;无符号0到255;默认是有符号 bool,noollean 使用0或1表示真或假 smallint[unsigned] 带符号是负的2的15次方到2的15次方-1 ,无符号2的16方 -1 int[unsigned] 带符号是到2的31次方-1,负的2的31次方无符号2的32方 -1 bigint[unsigned] 带符号是负的2的63次方到2的63次方-1,无符号2的64方 -1 (2)时间类型 date() 日期型;3字节;日期型,格式:YYYY-MM-DD time() 时间型;3字节;时间值,格式:HH:MM:SS numeruc(n,m); 精确数值类型;最多存储38个数字;精确存储数值,n定义数值个数,m定义小数位 TimeStamp 表示时间戳,它可用于自动记录insert、update操作的时间 (3)文本、二进制类型 char 字符型 定长;一个字母是1字符;0-255字节 varchar 字符型 变长;0-65535字节;存储速度 char比 varchar快 blob longblob (4)二进制数据 text longtext 大文本,不支持全文索引,不支持默认值,建议使用varchar (5)字段约束 create table 表名(字段1 字段类型 字段约束,字段2 字段类型 字段约束,字段3…); 例:创建一个stu表存放学生的学号,姓名,性别,年龄信息,并设置学号为主键 create table student(id int primary key,name char(20) ,sex char(10),age int); primary key 主键(特性:字段值不能重复,不能为空,表中只能有一个主键) not null 值不能是null值 unique 字段值不能重复(可多个) default 默认值;唯一性;数字:default 123 ;字符:default ‘中国’ auto_increment 用于主键自增长</code></pre>

页面列表

ITEM_HTML