mysql基本语句1

                            DDL语句(Data Definition Language)数据定义语言


1.显示数据库/表

         Showdatabases/tables;

2.创建数据库

         Createdatabase 数据库名;

3.删除数据库

         Dropdatabase数据库名;

4.进入数据库

         Use数据库名;

5.查看表的结构

         Desc表名;

6.建表

         Createtable 表名

         (

                   Column_name1datatype [default expr],

                   Column_name2datatype [default expr],

                   Column_name3datatype [default expr],

                   Aaa  varchar(255) default ‘XXXX’,

                   Bbb  int  

         );

7.子查询建表

         Createtable 表名1  as  select  * from 表名2;

8.修改表的结构

         Altertable 表名

         1.增加字段

                   Altertable 表名

                            Addcolumn_name1 datatype default ‘XXX’;(多个字段用括号包起来)

        

         2.修改列定义

                   Altertable 表名

                            Modifycolumn_name datatype default ‘YYY’ after column_name2;(不支持同时修改多个列定义,需要使用多个modify;修改不会对之前的字段造成影响)

         3.删除列

                   Altertable 表名

                            Drop  column_name;

 

(上面是标准的SQL语法,MySQL 特别提供的)

         4.表名重新命名

                   Altertable 表名

                            Renameto 表名1;

                  (Alter table test

                            Renameto rename;)

         5.列名改变

                   Altertable 表名

                            Change  oldcolumn_name newcolumn_name datatype aftercolumn_name1;

9.删除表

         Drop  table 表名;

                  

10.截断表(保留表的结构,删除表里的数据)

         Truncate表名;

 

                                     数据库约束

1.NOT NULL:非空约束;

2.UNIQUE:唯一约束,某列或者几列组合不能重复;

3.PRIMARY KEY:主键;

4.FOREIGN KEY:外键,

5.CHECK:检查,布尔表达式,指定对应列必须满足的表达式。

 

1.NOT  NULL:非空约束:

         (与java类似,空字符串不等于NULL,0也不等于NULL)

         1.直接在列定义后加notnull)

         createtable haha

                   Haha_id  int not null;

         2.alter table 时修改

         altertable haha

                   Modifyhaha_id int not null;

 

         3.取消非空约束,

                   alter  table haha

                            modify  haha_id int deafault ‘abc’ null ;      

 

2.UNIQUE:唯一约束,某列或者几列组合不能重复:

         (指定某列或者几列不能出现不允许出现重复值,但可以有多个null值,null不等于null

         1.使用列级约束语法

                   create  table unique_test

                   (

                            test_idint not null,

                            test_namevarchar(255) unique

                   );

 

        

 

 

 

2.使用表级约束语法

                   createtable unique_test2

                   (

                            test_idint not null,

                            test_namevarchar(255),        

                            test_passvarchar(255)

                            #表级约束语法

                            unique  (test_name),

                            #表级约束语法,并指定约束名test2_uk

                            constraint  test2_uk unique(test_pass), ①

                            #使用表级约束语法建立唯一约束,指定列组合不能为空

                            #constraint  test3_uk unique(test_name,test_pass) ②

                   );

                   注:①和②不能同时存,test2_uk要求test_name、test_pass都不能出现重复值,                  test3_unique要求test_name和test_pass两列的组合不能出现重复值

3.其他方法

         add关键字:

                   altertable unique_test2

                            add  unique (test_name,test_pass);

         modify关键字:

                   altertable unique_test2

                            modifytest_name varchar(255) unique;

4.删除约束

         对大多数数据库而言,删除约束都是在alter table语句后使用“drop  constraint  约束  名”,而Mysql 使用“drop index 约束名”的方式来删除约束:

        

         alter  table unique_test2

                   dropindex test2_uk;

 

3.PRIMARY  KEY:主键:

       主键:不能出现唯一值,也不能为空;每个表只允许出现一个主键,但是主键可以是多个列数据组合而成;不管是用户是否指定约束名,MYSQL总是将所有主键约束命名为PRIMARY。

        

         1.列级约束语法:

                   create  table primarykey_test1

                   (

                            test_id  int  primaryket,

                            test_name  varchar(255)

                   );

 

 

         2.表级约束语法:

                   create  table primarykey_test2

                   (

                            test_id  int not null,

                            test_namevarchar(255),

                            test_passvarchar(255),

                            primary key(test_id)

                            #建立多列组合主键,只能使用表级约束

                            #primary  key(test_pass,test_name)

                   );

         3.增加主键约束

                   altertable primarykey_test2

                            addprimary key(test_name,test_pass);

                  

                   altertable primarykey_test2

                            modify test_name  varhcar(255) primary key;

                           

         4.删除主键约束:

                   alter  table primarykey_test2

                            dropprimary key;

 

         5.自增长:

                   数据类型是整型,且是作为主键,则可让该列具有自增长的功能。

                   create  test primarykey_test3

                   (

                            test_idint auto_increment primary key);

 

4.FOREIGN  KEY:外键:

         外键主要用于一个或者两个表的数据之间的参照完整性:子表外键列的值必须在主表被参照列的值范围之内,或者为空。

         删除主表被参照记录时,要先删除从(子)表参照的记录,或者删除主表记录时级联删除参照该记录的所有从表记录。

         从表外键参照的只能是主表主键或者唯一键列,同一个表可以有多个外键。

         MYSQL支持列级和表级约束语法,但是列级约束语法不会生效!

         1.列级约束语法,references关键字

                   create  table  teacher_table1

                   (

                            teacher_id  int auto_increment ,

                            teacher_name  varchar(255),

                            primarykey(teacher_id)

                   );

                   create  table stu_table1

                   (

                            stu_id  int auto_increment  primary  key,

                            stu_name  varchar(255),

                            java_teacher  int  references  teacher_table1(teacher_id)

                   );(列级约束语法无效)

 

         2.表级约束语法,关键字foreignkey

                   create  table teacher_table2

                   (

                            teacher_id  int auto_increment ,

                            teacher_name  varchar(255),

                            primarykey(teacher_id)

                   );

                   create  table stu_table2

                   (

                            stu_id int auto_increment  primary  key,

                            stu_name  varchar(255),

                            java_teacher  int,

                            #表级约束,并指定约束名

                            constraint  student_teacher_fk  foreign key (java_teacher) references                                    teacher_table2(teacher_id)

                   );

         3.多列组合的外键约束:

                   create  table teacher_table3

                   (

                            teacher_passvarchar(255) ,

                            teacher_name  varchar(255),

                            primarykey(teacher_pass,teacher_name)

                   );

                   create  table stu_table3

                   (

                            stu_id int auto_increment  primary  key,

                            stu_name  varchar(255),

                            java_teache_pass  varchar(255),

                            java_teacher_name  varchar(255),

                            foreign  key (java_teacher_pass,java_teacher_name)  references                                                teacher_table3(teacher_pass,teacher_name)

                   );

 

         4.删除约束,drop:

                   alter  table stu_table3

                            drop  foreign key  stu_table3_ibfk_1;

        

        

 

         5.增加外键,add:

                   alter  table stu_table3

                            add  foreign key(java_teacher_pass, java_teacher_name)  references                                    teacher_table3(teacher_pass,teacher_name);

 

         6.自参照、自关联

                   同一个表中的两列

 

         7.删除从表记录:

                   on  delete cascade(删除主表记录时同时删除从表的记录)/on  delete  set null              (删除主表记录时同时设置从表的记录为空)

                   create  table teacher_table4

                   (

                            teacher_passvarchar(255) ,

                            teacher_name  varchar(255),

                            primarykey (teacher_pass,teacher_name)

                   );

                   create  table stu_table4

                   (

                            stu_id int auto_increment  primary  key,

                            stu_name  varchar(255),

                            java_teache_pass  varchar(255),

                            java_teacher_name  varchar(255),

                            foreign  key(java_teacher_pass,java_teacher_name) references                                                teacher_table4(teacher_pass,teacher_name)on  delete  cascade

                            #on  delete  set null

                   );

        

5.CHECK:检查,布尔表达式,指定对应列必须满足的表达式:

                   create  table check_test

                   (

                            emp_id  int auto_increment  primary key,

                            emp_salary  decimal,

                            check(emp_salary>0)

                   );

                   注:MYSQL里CHECK约束不会器任何作用!!!

 

                                                              索引

作用:加速对表的查询。

1.创建索引

         create  index  index _name on table_name(column_name1,colum_name2,……);

 

 

2.删除索引

         drop index  index_name  on table_name;

        

                                                                  视图

作用:一个或者多个数据表中数据的逻辑显示,本质上就是一条被命名的SQL查询语句。

 

1.创建视图

         create or  replace  view view_test

         as

         select teacher_name,teacher_pass from  teacher_table

         #指定不允许修改该视图的数据

         with check  option;

2.删除视图

         drop view  view_test;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值