首页 > 技术文章 > SQL基本操作

xp1005 2017-03-27 14:55 原文

1、数据库操作

show databases;
use [databasename];
create database  [name];

2、数据表操作

show tables;
 
create table students
    (
        id int  not null auto_increment primary key,
        name char(8) not null,
        sex char(4) not null,
        age tinyint unsigned not null,
        tel char(13) null default "-"
    );
1 CREATE TABLE `wb_blog` ( 
2     `id` smallint(8) unsigned NOT NULL, 
3     `catid` smallint(5) unsigned NOT NULL DEFAULT '0', 
4     `title` varchar(80) NOT NULL DEFAULT '', 
5     `content` text NOT NULL, 
6     PRIMARY KEY (`id`), 
7     UNIQUE KEY `catename` (`catid`) 
8 ) ; 
create table

3、数据基本操作

insert into students(name,sex,age,tel) values('alex','man',18,'151515151')
 
delete from students where id =2;
 
update students set name = 'sb' where id =1;
 
select * from students

4、删除数据的两种办法

1 1、假设删除第3行,后面还有第4,5行,使用delete删除后,新建行从6开始
2 
3 delete from table1 where id=3;
4 
5 2、清空所有数据,再次插入时,顺序从1开始
6 
7 truncate table table1;

 

5、外键

方式一:创建表时创建
mysql> create table  if not exists user_type(
    type_id int not null auto_increment primary key,
    permission char(6) null default "nomal"
    );

mysql> create table if not exists user_info(
    user_id int not null auto_increment primary key,
    username char(20) null default "-",
    password char(20) null default "-",
    perm_id int null default 1,
    foreign key(perm_id ) references user_type(type_id) on delete cascade on update cascade
    );

6、外键关系查询

 1 mysql> select username,password,permission
 2  from user_info left join user_type on (perm_id = type_id);
 3 +----------+----------+------------+
 4 | username | password | permission |
 5 +----------+----------+------------+
 6 | momo     | abc123   | nomal      |
 7 | t1       | abc123   | nomal      |
 8 | momomo   | abc123   | admin      |
 9 +----------+----------+------------+
10 3 rows in set

 7、用户赋权

# 赋予root@192.168.0.1 所有库的权限,并配置密码
grant all privileges on *.* to root@192.168.0.1 identified by 'wtfpass123' ;

# 赋予username@192.168.0.1 newdb库的权限
grant all privileges on newdb.* to username@192.168.0.1;

8、修改表

添加列:
    alter table 表名 add 列名 类型
删除列:
    alter table 表名 drop column 列名
修改列:
        alter table 表名 modify column 列名 类型;  -- 类型
        alter table 表名 change 原列名 新列名 类型; -- 列名,类型
  
添加主键:
        alter table 表名 add primary key(列名);
删除主键:
        alter table 表名 drop primary key;
        alter table 表名  modify  列名 int, drop primary key;
  
添加外键:
    alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);

删除外键:
    alter table 表名 drop foreign key 外键名称
  
修改默认值:
    ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;
删除默认值:
    ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;

9、基本数据类型

  1 bit[(M)]
  2             二进制位(101001),m表示二进制位的长度(1-64),默认m=1
  3 
  4         tinyint[(m)] [unsigned] [zerofill]
  5 
  6             小整数,数据类型用于保存一些范围的整数数值范围:
  7             有符号:
  8                 -128127.
  9             无符号:
 10                 0255
 11 
 12             特别的: MySQL中无布尔值,使用tinyint(1)构造。
 13 
 14         int[(m)][unsigned][zerofill]
 15 
 16             整数,数据类型用于保存一些范围的整数数值范围:
 17                 有符号:
 18                     -21474836482147483647
 19                 无符号:
 20                     04294967295
 21 
 22             特别的:整数类型中的m仅用于显示,对存储范围无限制。例如: int(5),当插入数据2时,select 时数据显示为: 00002
 23 
 24         bigint[(m)][unsigned][zerofill]
 25             大整数,数据类型用于保存一些范围的整数数值范围:
 26                 有符号:
 27                     -92233720368547758089223372036854775807
 28                 无符号:
 29                     018446744073709551615
 30 
 31         decimal[(m[,d])] [unsigned] [zerofill]
 32             准确的小数值,m是数字总个数(负号不算),d是小数点后个数。 m最大值为65,d最大值为30。
 33 
 34             特别的:对于精确数值计算时需要用此类型
 35                    decaimal能够存储精确值的原因在于其内部按照字符串存储。
 36 
 37         FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]
 38             单精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数。
 39                 无符号:
 40                     -3.402823466E+38 to -1.175494351E-38,
 41                     0
 42                     1.175494351E-38 to 3.402823466E+38
 43                 有符号:
 44                     0
 45                     1.175494351E-38 to 3.402823466E+38
 46 
 47             **** 数值越大,越不准确 ****
 48 
 49         DOUBLE[(M,D)] [UNSIGNED] [ZEROFILL]
 50             双精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数。
 51 
 52                 无符号:
 53                     -1.7976931348623157E+308 to -2.2250738585072014E-308
 54                     0
 55                     2.2250738585072014E-308 to 1.7976931348623157E+308
 56                 有符号:
 57                     0
 58                     2.2250738585072014E-308 to 1.7976931348623157E+308
 59             **** 数值越大,越不准确 ****
 60 
 61 
 62         char (m)
 63             char数据类型用于表示固定长度的字符串,可以包含最多达255个字符。其中m代表字符串的长度。
 64             PS: 即使数据小于m长度,也会占用m长度
 65         varchar(m)
 66             varchars数据类型用于变长的字符串,可以包含最多达255个字符。其中m代表该数据类型所允许保存的字符串的最大长度,只要长度小于该最大值的字符串都可以被保存在该数据类型中。
 67 
 68             注:虽然varchar使用起来较为灵活,但是从整个系统的性能角度来说,char数据类型的处理速度更快,有时甚至可以超出varchar处理速度的50%。因此,用户在设计数据库时应当综合考虑各方面的因素,以求达到最佳的平衡
 69 
 70         text
 71             text数据类型用于保存变长的大字符串,可以组多到65535 (2**161)个字符。
 72 
 73         mediumtext
 74             A TEXT column with a maximum length of 16,777,215 (2**241) characters.
 75 
 76         longtext
 77             A TEXT column with a maximum length of 4,294,967,295 or 4GB (2**321) characters.
 78 
 79 
 80         enum
 81             枚举类型,
 82             An ENUM column can have a maximum of 65,535 distinct elements. (The practical limit is less than 3000.)
 83             示例:
 84                 CREATE TABLE shirts (
 85                     name VARCHAR(40),
 86                     size ENUM('x-small', 'small', 'medium', 'large', 'x-large')
 87                 );
 88                 INSERT INTO shirts (name, size) VALUES ('dress shirt','large'), ('t-shirt','medium'),('polo shirt','small');
 89 
 90         set
 91             集合类型
 92             A SET column can have a maximum of 64 distinct members.
 93             示例:
 94                 CREATE TABLE myset (col SET('a', 'b', 'c', 'd'));
 95                 INSERT INTO myset (col) VALUES ('a,d'), ('d,a'), ('a,d,a'), ('a,d,d'), ('d,a,d');
 96 
 97         DATE
 98             YYYY-MM-DD(1000-01-01/9999-12-31 99 
100         TIME
101             HH:MM:SS('-838:59:59'/'838:59:59'102 
103         YEAR
104             YYYY(1901/2155105 
106         DATETIME
107 
108             YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59    Y)
109 
110         TIMESTAMP
111 
112             YYYYMMDD HHMMSS(1970-01-01 00:00:00/2037 年某时)
sql数据类型

10、提交

执行命令提交
commit;

将数据读取到内存中,从而立即生效
flush privileges;

11、创建用户

创建用户
    create user '用户名'@'IP地址' identified by '密码';
删除用户
    drop user '用户名'@'IP地址';
修改用户
    rename user '用户名'@'IP地址'; to '新用户名'@'IP地址';;
修改密码
    set password for '用户名'@'IP地址' = Password('新密码')

   

 

小知识

面试题:你的数据库用什么存储引擎?区别是?

答案:常见的有MyISAM和InnoDB。

MyISAM:不支持外键约束。不支持事务。对数据大批量导入时,它会边插入数据边建索引,所以为了提高执行效率,应该先禁用索引,在完全导入后再开启索引。

InnoDB:支持外键约束,支持事务。对索引都是单独处理的,无需引用索引。

  

推荐阅读