mysql創庫創表語句 mysql 資料庫(1)

2021-10-19 19:13:12 字數 3825 閱讀 3669

----------------1----------------------

1.掌握創庫,創表的方法

創庫create database haha;

使用庫use haha;

創表1create table t1 (id int);

檢視所有表

show tables;

插入資料

insert into t1 values(1);

查詢所有資料

select * from t1;

刪除表drop table t1;

----------------2----------------------

2.掌握建立多列**的方法。

創表2(兩列id 和 name(16長度))

create table t2 (id int,name varchar(16));

檢視表結構

desc t2;

插入資料

insert into t2 values(1,"zhangsan");

insert into t2 values (2,"lisi"),(3,"wangwu"); 可以一次插入多個資料

查詢所有資料

select * from t2;

----------------3---------------------

3.掌握修改表名的方法,以及增加新列的方法。

修改表名

alter table t2 rename t3;

檢視所有表

show tables;

增加一列

alter table t3 add address varchar (50);

檢視表結構

desc t3;

插入資料

insert into t3 values(1,"wangwu","beijing");

查詢所有資料

select * from t3;

----------------4---------------------

4.掌握插入新列的方法。

在id列前增加一列(idd int)

alter table t3 add idd int first;

在id列後增加一列(*** char)

alter table t3 add *** char after id;

檢視表結構

desc t3;

---------------5-----------------------

5.掌握刪除列的方法。

刪除idd列

alter table t3 drop idd ;

檢視表結構

desc t3;

---------------6------------------------

6.掌握修改列名的方法。

檢視表結構

desc t3;

修改name列的列名為mingzi

alter table t3 change name mingzi varchar(16) ;

檢視表結構

desc t3;

---------------7--------------------------

7.掌握複製表的方法。

把t3 複製 t4

create table t4 select * from t3;

檢視t3 和t4 的內容

select from t3;

select from t4;

--------------8-------------------------

8.列的型別-整數型測試:tinyint,int

創表mysql> create table test1(

tinyint_test tinyint,

int_test int

插入兩列合法值

mysql> insert into test1 values (111,111);

query ok, 1 row affected (0.09 sec)

插入tinyint非法值

錯誤的示例:

mysql> insert into test1(tinyint_test) values(128);

error 1264 (22003): out of range value for column 'tinyint_test' at row 1

正確的示例:

mysql> insert into test1(tinyint_test) values(127);

插入int合法值

mysql> insert into test1(int_test) values(2147483647);

query ok, 1 row affected (0.05 sec)

插入int的非法值

mysql> insert into test1(int_test) values(2147483648);

error 1264 (22003): out of range value for column 'int_test' at row 1

驗證那些值插入成功?

select * from test1;

--------------9-------------------------

9.數值列的符號概念。

創表create table test2(

tinyint_test tinyint unsigned,

int_test int unsigned

插入tinyint unsigned 合法值

insert into test2(tinyint_test) values(255);

插入int unsigned 合法值

mysql> insert into test1(int_test) values(2147483648);

插入兩列的非法數值 負數。

mysql> insert into test2 values(-20,-20);

error 1264 (22003): out of range value for column 'tinyint_test' at row 1

--------------10-------------------------

10.掌握整數型 零填充約束。

創表create table t2 (

id1 int zerofill,

id2 int(6) zerofill

插入值mysql> insert into t2 values(2,2);

query ok, 1 row affected (0.01 sec)

查詢值mysql> select * from t2;

| id1 | id2 |

| 0000000002 | 000002 |

1 row in set (0.00 sec)

--------------11-------------------------

11.掌握小數型資料型別

創表create table test4(float_test float(5,2));

插入數值

insert into test4 values (10.2), (70.243), (70.246);

查詢結果

mysql> select * from test4;

| float_test |

| 10.20 |

| 70.24 |

| 70.25 |

3 rows in set (0.01 sec)

------------12--------------------

sql建表語句轉成mysql sql語句建立成登入

sql語句建立成登入 1 windows登入賬戶口 exec ap grantlogin windows網域名稱 域賬戶 2 sql 登入賬戶 exec sp addlogin 賬戶名 密碼 3 建立資料庫使用者 exec spgrantdbaccess 登入賬戶 資料庫使用者 二 給資料庫使用者授...

mysql創標識列語句 mysql 標識列

標識列 又稱為自增長列 含義 可以不用手動插入值,系統提供預設的序列值 特點 1.表示列必須和主鍵搭配嗎?不一定,但是要求是乙個key 2.乙個表中只能有乙個標識列!3.標識列的型別有限制嗎?只能是數值型別 int,float,double 4.標識列可以通過set auto increment i...

mysql建表語句

在sql語句中注意 約束的概念 1.實體完整性約束 主鍵 唯一且非空 primary key 違約處理 no action 拒絕執行 2.參照完整性約束 外來鍵約束 foregin key references tablename filedname on delete update casecad...